1 // UIView动画分析 2 3 #import "ViewController.h" 4 5 @interface ViewController () 6 7 @property (nonatomic,strong) UIView *myView; 8 9 @end 10 11 @implementation ViewController 12 13 14 15 // 懒加载是get方法啊 16 -(UIView *)myView{ 17 if (_myView == nil) { 18 _myView = [[UIView alloc]init]; 19 _myView.backgroundColor = [UIColor orangeColor]; 20 _myView.frame = CGRectMake(100, 100, 100, 100); 21 } 22 return _myView; 23 } 24 25 26 27 -(void)viewDidLoad { 28 [super viewDidLoad]; 29 self.myView.center = CGPointMake(self.view.center.x, 0); 30 [self.view addSubview:_myView]; 31 } 32 33 -(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{ 34 35 NSLog(@"begin"); 36 [UIView animateWithDuration:2.0 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:10.0 options:0 animations:^{ 37 NSLog(@"开始动画"); 38 self.myView.center = self.view.center; 39 } completion:^(BOOL finished) { 40 NSLog(@"完成动画"); 41 }]; 42 NSLog(@"over"); 43 44 } 45 @end
结论:
block是一个C语言的预先准备好的代码在需要的时候执行。而UIView系统封装的动画是立即执行。所以在回调中写了代码后就立马执行的。一开始是begin,接下来是开始动画,然后异步over,再完成动画。
时间: 2024-11-16 12:45:48