AutoLayout动画

2020-11-02  本文已影响0人  我不白先生
image.png
image.png

ViewController.m

@interface ViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
@property(nonatomic,assign)CGFloat constant;
@end
@implementation ViewController
//开始触摸啊Set集合
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *pTouch = touches.anyObject;
    NSLog(@"touchesBega %@", NSStringFromCGPoint([pTouch locationInView:self.view]));
}
//移动 只要移动 就会调用该方法
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *pTouch = touches.anyObject;
      NSLog(@"touchesMoved %@",NSStringFromCGPoint([pTouch locationInView:self.view]));
    self.topConstraint.constant = [pTouch locationInView:self.view].y;
}
//结束触摸 抬起了
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *pTouch = touches.anyObject;
    NSLog(@"touchesEnded %@",NSStringFromCGPoint([pTouch locationInView:self.view]));
    [UIView animateWithDuration:1 animations:^{
        //抬起时 恢复到 开始的约束值
        self.topConstraint.constant = self.constant;
        //对约束做动画,约束最终位置的设置,在animation代码块中或代码块外都是可以的
        //但是在 animations代码块中 一定要调用父视图 layoutIfNeeded方法,在动画过程中不断更新约束核心self.view layoutIfNeeded
        [self.view layoutIfNeeded];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //保存约束 开始的值
    self.constant = self.topConstraint.constant;
}
上一篇下一篇

猜你喜欢

热点阅读