标签:
#import "ViewController.h" @interface ViewController () @property (strong,nonatomic)CALayer *subLayer; //声明核心动画子层 @end
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建子层 self.subLayer = [CALayer layer]; self.subLayer.bounds = CGRectMake(100, 100, 100, 100); self.subLayer.position = CGPointMake(100, 100); self.subLayer.backgroundColor = [[UIColor redColor]CGColor]; [self.view.layer addSublayer:self.subLayer]; //添加触摸手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)]; tap.numberOfTapsRequired = 1; tap.numberOfTouchesRequired = 1; [self.view addGestureRecognizer:tap]; }
#pragma mark 返回一个实现旋转的基本动画 -(CABasicAnimation*)rotationAnimationFromValue:(CGFloat)fromValue toValue:(CGFloat)tovalue { //创建基本动画(用于旋转) CABasicAnimation *baseAnimation = [[CABasicAnimation alloc]init]; //设置形变属性值为旋转属性值 baseAnimation.keyPath = @"transform.rotation.z"; //设置旋转初值 baseAnimation.fromValue = @(fromValue); //设置旋转终值 baseAnimation.toValue = @(tovalue); //设置旋转动画持续时间 baseAnimation.duration = 1.0f; //设置动画旋转结束后不恢复原状 baseAnimation.removedOnCompletion = NO; baseAnimation.fillMode = kCAFillModeForwards; return baseAnimation; }
#pragma mark 返回一个实现放缩的基本动画 -(CABasicAnimation*)scaleAnimationFromValue:(CGFloat)fromValue toValue:(CGFloat)tovalue { //创建基本动画(用于放缩) CABasicAnimation *animScale = [[CABasicAnimation alloc]init]; //设置形变属性为放缩属性值 animScale.keyPath = @"transform.scale"; //设置放缩初值 animScale.fromValue = @(fromValue); //设置放缩终值 animScale.toValue = @(tovalue); //设置放缩动画持续时间 animScale.duration = 1.0f; //设置动画放缩结束后不恢复原状 animScale.removedOnCompletion = NO; animScale.fillMode = kCAFillModeForwards; return animScale; }
#pragma mark -tap触摸事件 -(void)tap:(UITapGestureRecognizer *)sender { //方式一:依次调用各个的动画,效果也是可以的 //旋转360度 //[self.subLayer addAnimation:[self rotationAnimationFromValue:-M_PI toValue:M_PI] forKey:@"baseAnimation"];
//放大2倍 //[self.subLayer addAnimation:[self scaleAnimationFromValue:1.0 toValue:2.0] forKey:@"animScale"];
//方式二:并发执行动画组中的所有动画 //创建动画组 CAAnimationGroup *Group = [[CAAnimationGroup alloc]init]; //将各种动画对象加入数组中 NSArray *animations = @[[self rotationAnimationFromValue:-M_PI toValue:M_PI],[self scaleAnimationFromValue:1.0 toValue:2.0]]; //动画数组中的动画的属性受动画组的统一控制 Group.animations = animations; //设置动画组中所有动画的持续时间 Group.duration = 1.0; //设置动画组中所有动画运行结束后不恢复原状 Group.removedOnCompletion = NO; Group.fillMode = kCAFillModeForwards; //往子层中添加动画组 [self.subLayer addAnimation:Group forKey:@"Group"]; }
标签:
原文地址:http://www.cnblogs.com/XYQ-208910/p/4886576.html