[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];//交换位置
5、在commitAnimations消息之前,可以设置动画完成后的回调,设置方法是:
[UIView setAnimationDidStopSelector:@selector(animationFinish:)];
三、使用:CATransition
1、创建动画
CATransition *ansition = [CATransitionanimation];
2、动画持续时间
ansition.duration = 2;
3、 动画循环次数
ansition.repeatCount = 2;
4、 设置动画类型
ansition.type = @"cameraIrisHollowOpen";
5、 设置动画方向
ansition.subtype = kCATransitionFromLeft;
6、设置动画代理
ansition.delegate = self;
7、替换
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
8、添加动画
[view.layeraddAnimation:ansition forKey:@"12"];
9、animateWithDuration
[UIView animateWithDuration:1 animations:^{
//要执行的操作
_view.frame = CGRectMake(20, 140, 200, 100);
}];
[UIView animateWithDuration:1 animations:^{
//要执行的操作
} completion:^(BOOL finished) {
动画结束
}];
(1)transition.type 的类型可以有
淡化、推挤、揭开、覆盖
NSString * const kCATransitionFade;
NSString * const kCATransitionMoveIn;
NSString * const kCATransitionPush;
NSString * const kCATransitionReveal;
(2)transition.subtype
也有四种
NSString * const kCATransitionFromRight;
NSString * const kCATransitionFromLeft;
NSString * const kCATransitionFromTop;
NSString * const kCATransitionFromBottom;
(3)私有的类型的动画类型:
animation.type = @”cube”
animation.type = @”suckEffect”
animation.type = @”oglFlip”//没有方向
animation.type = @”rippleEffect”
animation.type = @”pageCurl”
animation.type = @”pageUnCurl”
animation.type = @”cameraIrisHollowOpen”
animation.type = @”cameraIrisHollowClose”
四、具体代码
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(50, 50, 100, 30);
//btn1.backgroundColor = [UIColor blackColor];
[btn1 setTitle:@"动画一" forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(btn1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(120, 50, 100, 30);
[btn2 setTitle:@"动画二" forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(btn2) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
btn3.frame = CGRectMake(190, 50, 100, 30);
[btn3 setTitle:@"动画三" forState:UIControlStateNormal];
[btn3 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[btn3 addTarget:self action:@selector(btn3) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn3];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(375/2.0-50, 300, 100, 100)];
view.tag = 1;
view.backgroundColor = [UIColor greenColor];
[self.view addSubview:view];
}
-(void)btn1{
UIView *view = [self.view viewWithTag:1];
[UIView beginAnimations:@"1" context:nil];//创建动画
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];//动画曲线
[UIView setAnimationDelay:0];//延时动画
[UIView setAnimationDuration:2];//动画持续时间
[UIView setAnimationRepeatCount:2];//动画循环次数
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:YES];//设置动画效果,以及要执行的view(动画发生位置,此处是view)
[UIView setAnimationDelegate:self];//设置动画代理
[UIView setAnimationDidStopSelector:@selector(stop)];//动画结束后调用的方法,注意设置次方法之前要先设置代理
[UIView commitAnimations];//提交动画
}
-(void)btn2{
UIView *view = [self.view viewWithTag:1];
CATransition *ansition = [CATransition animation];//创建动画
ansition.duration = 2;//动画持续时间
//ansition.repeatCount = 2;//动画循环次数
ansition.type = @"cameraIrisHollowOpen";//设置动画类型
ansition.subtype = kCATransitionFromLeft;//设置动画方向
ansition.delegate = self;//设置动画代理
[view.layer addAnimation:ansition forKey:@"12"];//添加动画
}
-(void)btn3{
UIView *view = [self.view viewWithTag:1];
CGPoint center = view.center;
//放大效果
// [UIView animateWithDuration:1 animations:^{
// view.frame = CGRectMake(10, 10, 300, 300);
// view.center = center;
//
// }];
//放大缩小连续
[UIView animateWithDuration:1 animations:^{
view.frame = CGRectMake(10, 10, 300, 300);
view.center = center;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1 animations:^{
view.frame = CGRectMake(375/2.0, 300, 100, 100);
view.center = center;
} completion:^(BOOL finished) {
[self btn3];//添加动画
}];
}];
//实现缩小和放大
[UIView animateWithDuration:1 delay:1 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
view.frame = CGRectMake(10, 10, 300, 300);
view.center = center;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1 animations:^{
view.frame = CGRectMake(375/2.0, 300, 100, 100);
view.center = center;
}completion:^(BOOL finished) {
[self btn3];
}];
}];
}
-(void)stop1{
NSLog(@"动画停止");
}
-(void)animationDidStart:(CAAnimation *)anim{
NSLog(@"动画开始");
}