#####摘要:执行动画的本质是改变图层的属性git
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //开启动画 CABasicAnimation *basic=[CABasicAnimation animation]; //描述哪一个属性产生动画 basic.keyPath=@"position"; //设置值 basic.toValue=[NSValue valueWithCGPoint:CGPointMake(200, 450)]; //设置动画完成时候再也不移除动画 basic.removedOnCompletion=NO; //设置动画执行完成要保持最新的效果 basic.fillMode=kCAFillModeForwards; //添加动画 [self.greenView.layer addAnimation:basic forKey:nil]; }
#####补充:github
#####2.CAKeyframeAnimation数组
- (void)drawRect:(CGRect)rect { [self.path stroke]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ UITouch *touch=[touches anyObject]; CGPoint p=[touch locationInView:self]; self.path=[UIBezierPath bezierPath]; [self.path moveToPoint:p]; } -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ UITouch *touch=[touches anyObject]; CGPoint p=[touch locationInView:self]; [self.path addLineToPoint:p]; [self setNeedsDisplay]; } -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //开启动画 // CAKeyframeAnimation *keyFrame=[[CAKeyframeAnimation alloc]init]; CAKeyframeAnimation *keyFrame=[CAKeyframeAnimation animation]; keyFrame.keyPath=@"position"; keyFrame.path=self.path.CGPath; keyFrame.duration=1; keyFrame.repeatCount=MAXFLOAT; //添加动画 [self.subviews.firstObject.layer addAnimation:keyFrame forKey:nil]; }
#####3.CATransition #####3.1 #####3.2代码架构
static int i=2; -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ if (i>3) { i=2; } NSString *imageName=[NSString stringWithFormat:@"%d",i]; self.imageView.image=[UIImage imageNamed:imageName]; CATransition *transaction=[CATransition animation]; transaction.type=@"pageCurl"; [self.imageView.layer addAnimation:transaction forKey:nil]; i++; }
#####4.CAAnimationGroup #####4.1注意并发
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ CAAnimationGroup *animGroups=[CAAnimationGroup animation]; CABasicAnimation *basic=[CABasicAnimation animation]; CABasicAnimation *basic2=[CABasicAnimation animation]; //removedOnCompletion无效果 basic.keyPath=@"position"; basic.toValue=[NSValue valueWithCGPoint:CGPointMake(150, 400)]; // basic.removedOnCompletion=NO; // basic.fillMode=kCAFillModeForwards; // basic2.keyPath=@"transform.scale"; basic2.toValue=@0.5; // basic2.removedOnCompletion=NO; // basic2.fillMode=kCAFillModeForwards; animGroups.animations=@[basic,basic2]; animGroups.removedOnCompletion=NO; animGroups.fillMode=kCAFillModeForwards; [self.redView.layer addAnimation:animGroups forKey:nil]; }
#####5.详细的源码地址(如下是github地址)动画