iOS核心动画(三个动画类的使用)

核心动画(三个动画类的使用)

#####摘要:执行动画的本质是改变图层的属性git

  • 1.Core Animation能够在Mac OSX和iOS中使用
  • 2.Core Animation的动画执行过程都是在后台操做的,不会阻塞主线程
  • 3.Core Animation是直接做用在CALayer上的,并不是UIView
  • 4.架构 #####1.CABasicAnimation
  • 设置动画属性(position),告诉动画执行怎样的动画
  • 设置动画属性值改变 toValue 从当前默认的开始,告诉动画属性怎么改变
  • duration:动画时长
  • 动画有反弹?取消反弹
    • 1> 执行动画完毕不要移除
    • 2> 设置动画填充模式,保持最新的位置。
  • CABasicAnimation只能在两个值之间作动画,用CAKeyframeAnimation,实现多个值作动画
-(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

  • fillMode属性值(要想fillMode有效,最好设置removedOnCompletion = NO)
  • kCAFillModeRemoved 这个是默认值,也就是说当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到以前的状态
  • kCAFillModeForwards 当动画结束后,layer会一直保持着动画最后的状态
  • kCAFillModeBackwards 在动画开始前,只须要将动画加入了一个layer,layer便当即进入动画的初始状态并等待动画开始。
  • kCAFillModeBoth 这个其实就是上面两个的合成.动画加入后开始以前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态

#####2.CAKeyframeAnimation数组

  • values:为NSArray对象。动画对象会在指定的时间(duration)内,依次显示values数组中的每个关键帧
  • path:能够设置一个CGPathRef,让图层按照路径轨迹移动。path只对CALayer的anchorPoint和position起做用。若是设置了path,那么values将被忽略
- (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注意并发

  • 动画组,是CAAnimation的子类,能够保存一组动画对象,将CAAnimationGroup对象加入层后,组中全部动画对象能够同时并发运行
-(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地址)动画

相关文章
相关标签/搜索