CATransition :转场动画 翻转动画
@interface ViewController () - (IBAction)previous:(UIButton *)sender; - (IBAction)next:(UIButton *)sender; @property (strong, nonatomic) IBOutlet UIImageView *iconView; @property(nonatomic,assign)int index;//当前图片的索引 @property (strong, nonatomic) IBOutlet UIView *myView; @end
/* 过渡效果
fade //交叉淡化过渡(不支持过渡方向) kCATransitionFade
push //新视图把旧视图推出去 kCATransitionPush
moveIn //新视图移到旧视图上面 kCATransitionMoveIn
reveal //将旧视图移开,显示下面的新视图 kCATransitionReveal
cube //立方体翻滚效果
oglFlip //上下左右翻转效果
suckEffect //收缩效果,如一块布被抽走(不支持过渡方向)
rippleEffect //滴水效果(不支持过渡方向)
pageCurl //向上翻页效果
pageUnCurl //向下翻页效果
cameraIrisHollowOpen //相机镜头打开效果(不支持过渡方向)
cameraIrisHollowClose //相机镜头关上效果(不支持过渡方向)
*/
//下一页
- (IBAction)next:(UIButton *)sender { self.index++; if (self.index==9) { self.index=0; } NSString * filename=[NSString stringWithFormat:@"%d.jpg",self.index+1]; self.iconView.image=[UIImage imageNamed:filename]; //转场动画 CATransition * anim=[CATransition animation]; //翻转的类型 anim.type=@"pageCurl";
//翻转方向 // anim.subtype=kCATransitionFromRight; //从多少进度开始翻 anim.startProgress=0.3; //到多少进度结束 anim.endProgress=0.5; [self.iconView.layer addAnimation:anim forKey:nil]; }
上一页
- (IBAction)previous:(UIButton *)sender { self.index--; if (self.index==-1) { self.index=8; } NSString * filename=[NSString stringWithFormat:@"%d.jpg",self.index+1]; self.iconView.image=[UIImage imageNamed:filename]; //转场动画 CATransition * anim=[CATransition animation]; //翻转的类型 anim.type=@"pageUnCurl"; //翻转方向 // anim.subtype=kCATransitionFromLeft; [self.iconView.layer addAnimation:anim forKey:nil]; }
CAAnimationGroup:组合动画
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //创建旋转动画对象 CABasicAnimation * rotate =[CABasicAnimation animation]; rotate.keyPath=@"transform.rotation"; rotate.toValue=@(M_PI); //创建缩放动画对象 CABasicAnimation * scale=[CABasicAnimation animation]; scale.keyPath=@"transform.scale"; scale.toValue=@(0.0); //添加动画 CAAnimationGroup * group =[CAAnimationGroup animation]; group.duration=2.0; group.removedOnCompletion=NO; group.fillMode=kCAFillModeForwards; group.repeatCount=MAX_CANON; group.animations=@[scale]; group.animations=@[rotate,scale]; [self.myView.layer addAnimation:group forKey:nil]; }