动画效果提供了状态或页面转换时流畅的用户体验,在iOS系统中,我们不须要本身编写绘制动画的代码,Core Animation提供了丰富的api来实现你须要的动画效果。html
UIKit只用UIView来展现动画,动画支持UIView下面的这些属性改变:ios
- (void)viewDidLoad { [super viewDidLoad]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setTitle:@"改变" forState:UIControlStateNormal]; button.frame = CGRectMake(10, 10, 60, 40); [button addTarget:self action:@selector(changeUIView) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)changeUIView{ [UIView beginAnimations:@"animation" context:nil]; [UIView setAnimationDuration:1.0f]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; [UIView commitAnimations]; }
UIViewAnimationTransitionNone, UIViewAnimationTransitionFlipFromLeft, UIViewAnimationTransitionFlipFromRight, UIViewAnimationTransitionCurlUp, UIViewAnimationTransitionCurlDown,
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];api
先添加两个view ,一个redview 一个yellowviewapp
- (void)viewDidLoad { [super viewDidLoad]; UIView *redView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; redView.backgroundColor = [UIColor redColor]; [self.view addSubview:redView]; UIView *yellowView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; yellowView.backgroundColor = [UIColor yellowColor]; [self.view addSubview:yellowView]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setTitle:@"改变" forState:UIControlStateNormal]; button.frame = CGRectMake(10, 10, 300, 40); [button addTarget:self action:@selector(changeUIView) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button1 setTitle:@"改变1" forState:UIControlStateNormal]; button1.frame = CGRectMake(10, 60, 300, 40); [button1 addTarget:self action:@selector(changeUIView1) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button1]; }
- (void)changeUIView1{ [UIView beginAnimations:@"animation" context:nil]; [UIView setAnimationDuration:1.0f]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES]; // 交换本视图控制器中2个view位置 [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0]; [UIView commitAnimations]; }
在commitAnimations消息以前,能够设置动画完成后的回调,设置方法是:ide
[UIView setAnimationDidStopSelector:@selector(animationFinish:)];动画
- (void)changeUIView2{ CATransition *transition = [CATransition animation]; transition.duration = 2.0f; transition.type = kCATransitionPush; transition.subtype = kCATransitionFromTop; [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0]; [self.view.layer addAnimation:transition forKey:@"animation"]; }transition.type 的类型能够有
淡化、推挤、揭开、覆盖url
NSString * const kCATransitionFade;spa
NSString * const kCATransitionMoveIn;.net
NSString * const kCATransitionPush;code
NSString * const kCATransitionReveal;
这四种,
NSString * const kCATransitionFromRight;
NSString * const kCATransitionFromLeft;
NSString * const kCATransitionFromTop;
NSString * const kCATransitionFromBottom;
立方体、吸取、翻转、波纹、翻页、反翻页、镜头开、镜头关
animation.type = @"cube" animation.type = @"suckEffect"; animation.type = @"oglFlip";//无论subType is "fromLeft" or "fromRight",official只有一种效果 animation.type = @"rippleEffect"; animation.type = @"pageCurl"; animation.type = @"pageUnCurl" animation.type = @"cameraIrisHollowOpen "; animation.type = @"cameraIrisHollowClose ";下图是第一个cube立方体的效果:
moveView = [[UIView alloc] initWithFrame:CGRectMake(10, 180, 200, 40)]; moveView.backgroundColor = [UIColor blackColor]; [self.view addSubview:moveView];
- (void)changeUIView3{ [UIView animateWithDuration:3 animations:^(void){ moveView.frame = CGRectMake(10, 270, 200, 40); }completion:^(BOOL finished){ UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 40, 40)]; label.backgroundColor = [UIColor blackColor]; [self.view addSubview:label]; }]; }而后用UIView animateWithDuration动画移动,移动动画完毕后添加一个Label。
- (void)changeUIView3{ [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^(void){ moveView.alpha = 0.0; }completion:^(BOOL finished){ [UIView animateWithDuration:1 delay:1.0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^(void){ [UIView setAnimationRepeatCount:2.5]; moveView.alpha = 1.0; }completion:^(BOOL finished){ }]; }]; }这个嵌套的效果是先把view变成透明,在从透明变成不透明,重复2.5次透明到不透明的效果。
容芳志 (http://blog.csdn.net/totogo2010)
本文遵循“署名-非商业用途-保持一致”创做公用协议
本文转载至 http://blog.csdn.net/totogo2010/article/details/8501812