iOS开发中的动画实现(一)UIView自带动画效果
在苹果App中,我们经常可以见到很多动画效果,例如颜色渐变,控件位移、缩放、旋转,页面切换样式等等。那么这些动画是如何实现的呢?
苹果为开发者们提供了很多实现动画效果的类和方法,我们可以通过这些方法实现我们所需要的效果。
1.UIView 自带的动画特效
在 UIView 中,自带有实现简单动画的方法,可以实现变色,位移,切换界面的效果:
1 #import "ViewController.h" 2 @interface ViewController () 3 @property(nonatomic,strong)UIView *view_; 4 @property(nonatomic,strong)UIView *view2; 5 @property(nonatomic,strong)UIView *view3; 6 @property(nonatomic,assign)BOOL change; 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 14 self.view_ = [[UIView alloc] initWithFrame:CGRectMake(100, 250, 100, 100)]; 15 self.view_.backgroundColor = [UIColor blueColor]; 16 [self.view addSubview:self.view_]; 17 18 self.view2 = [[UIView alloc] initWithFrame:self.view_.bounds]; 19 self.view2.backgroundColor = [UIColor grayColor]; 20 [self.view_ addSubview:self.view2]; 21 self.view3 = [[UIView alloc] initWithFrame:self.view_.bounds]; 22 self.view3.backgroundColor = [UIColor purpleColor]; 23 } 24 25 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 26 #pragma mark UIView_self_animation 27 28 // 1.1 Color_change 颜色渐变 29 [UIView beginAnimations:@"view_animation" context:nil]; 30 [UIView setAnimationDuration:1]; 31 self.view_.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1]; 32 33 // 1.2 Zoom_change 缩放 34 CGRect frame = self.view_.frame; 35 frame.size = CGSizeMake(frame.size.width*(arc4random()%101/50.0)+10, frame.size.height*(arc4random()%101/50.0)+10); 36 self.view_.frame = frame; 37 38 // 1.3 Position_move 位移 39 [UIView setAnimationRepeatAutoreverses:YES]; 40 [UIView setAnimationRepeatCount:1.5]; 41 frame.origin = CGPointMake((SC_WIDTH-100)*(arc4random()%101/100.0), (SC_HEIGHT-100)*(arc4random()%101/100.0)); 42 self.view_.frame = frame; 43 [UIView commitAnimations]; 44 45 // 1.4 View_mask/change 转场动画 46 if(!_change){ 47 _change = YES; 48 [UIView transitionWithView:self.view_ duration:1 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{ 49 if(self.view2.superview != self.view_){ 50 [self.view3 removeFromSuperview]; 51 [self.view_ addSubview:self.view2]; 52 }else{ 53 [self.view2 removeFromSuperview]; 54 [self.view_ addSubview:self.view3]; 55 } 56 } completion:^(BOOL finished) { 57 _change = NO; 58 }]; 59 }
在上面的代码中,我们通过点击界面实现 UIView自带动画的展示。在此,我们需要及用到:
①设置开启动画效果的方法:第一个参数为字符串,如"animation";第二个参数天nil即可
[UIView beginAnimations:(nullable NSString *) context:(nullable void *)]
②设置动画持续时间:参数为NSTimeInterval,实际上是double类型
[UIView setAnimationDuration:(NSTimeInterval)]
③设置控件是否自动回退:在控件进行位移时使用这个方法,可以使控件在进行重复动画(repeat)时又自动回退的效果
[UIView setAnimationRepeatAutoreverses:(BOOL)]
④设置动画重复次数:使动画特效重复执行多次,这里需要注意控件位移,需要设置为float类型n.5次,如果设置的是整数,控件会在位移后返回原坐标;设置为n.5次,控件会在多次重复执行后抵达目标坐标;
[UIView setAnimationRepeatCount:(float)]
⑤转场动画设置:设置你需要进行跳转的view,动画持续时间duration,跳转效果UIViewAnimationOptions以及在block中添加你需要处理的跳转事件
[UIView transitionWithView:(nonnull UIView *) duration:(NSTimeInterval) options:(UIViewAnimationOptions) animations:^(void)animations completion:^(BOOL finished)completion] [UIView transitionFromView:(nonnull UIView *) toView:(nonnull UIView *) duration:(NSTimeInterval) options:(UIViewAnimationOptions) completion:^(BOOL finished)completion]
⑥提交动画:在这里,使用该方法与否都不会影响动画效果,该方法的文档是这样说明的:标志着一个begin/commit包含的动画结束,并明确告知在iOS 4.0之后是不被鼓励使用的,苹果鼓励开发者使用基于block的动画效果实现
[UIView commitAnimations]