iOS开发之关键帧动画

        

  CABasicAnimation揭示了大多数隐式动画背后以来的机制,但是显示的给图层添加CABasicAnimation相较于隐式动画而言,之恩能够说是非礼不讨好。

  CAKeyFrameAnimation是一种UIKit没有暴露出来但是功能强大的类。和CABasicAnimation类似,CAKeyFrameAnimation同样是CAPropertyAnimation的一个子类,它依然作用于单一的一个属性,但是和CABasicAnimation不一样的是,它不限制设置一个起始和结束的值,而是可以根据一连串随意地值来做动画。

 关键帧起源于传动动画,意思是指主导的动画在显著改变发生时重绘当前帧,每帧之间剩下的绘制有系统来推算绘制完成 。CAKeyframeAnimation也是同样的道理:你提供了显著的帧(关键帧),剩下的由Core Animation在每帧之间插入。


#import "ViewController.h"

#define WIDTH   [[UIScreen mainScreen].bounds.size.width]

#define HEIGHT  [[UIScreen mainScreen].bounds.size.height]

@interface ViewController ()

@property (nonatomic,strong)UIView *containerView;

@property (nonatomic,strong)CALayer *  colorLayer;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

   //关键帧动画

       // [self customeLayer];

  //贝塞尔曲线

    [self    createBezierPath];

}

- (void)createBezierPath{


    self.containerView = [[UIViewalloc]initWithFrame:CGRectMake(10,100200,2000)];

    [self.viewaddSubview:self.containerView];

      //create a path

    UIBezierPath * bezierPath = [[UIBezierPathalloc]init];

    [bezierPath moveToPoint:CGPointMake(0,150)];

      //绘制曲线

    [bezierPath addCurveToPoint:CGPointMake(300,150controlPoint1:CGPointMake(75,0controlPoint2:CGPointMake(30,400)];

      //draw the path using a CAShapeLayer

    

    CAShapeLayer * pathLayer = [CAShapeLayerlayer];

    pathLayer.path = bezierPath.CGPath;

     //设置填充色

    pathLayer.fillColor = [UIColorwhiteColor].CGColor;

      //设置线条色

    pathLayer.strokeColor = [UIColorredColor].CGColor;

    pathLayer.lineWidth =3.0f;

    [self.containerView.layeraddSublayer:pathLayer];

    

    //add the ship

    CALayer * shipLayer = [CALayerlayer];

    shipLayer.frame =CGRectMake(0,064,64);

    shipLayer.position =CGPointMake(0,150);

    shipLayer.contents = (__bridgeid)[UIImageimageNamed:@"选择框"].CGImage;

    [self.containerView.layeraddSublayer:shipLayer];

    

    //create the keyframe animation

    CAKeyframeAnimation * animation = [CAKeyframeAnimationanimation];

     animation.keyPath =@"position";

    animation.duration =4.0f;

    animation.path = bezierPath.CGPath;

      //设置运动的方向

    animation.rotationMode =kCAAnimationRotateAuto;

    [shipLayer addAnimation:animationforKey:nil];


}

- (void)customeLayer{

    self.colorLayer = [CALayerlayer];

    self.colorLayer.frame =CGRectMake(100.f,100.f100.f,100.f);

    self.colorLayer.backgroundColor = [UIColorblueColor].CGColor;

    [self.view.layeraddSublayer:self.colorLayer];

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [selfCAKeyAnimation];

    });

}

- (void)CAKeyAnimation{


    CAKeyframeAnimation *animation = [CAKeyframeAnimationanimation];

    animation.keyPath =@"backgroundColor";

    animation.duration  =2.0;

    animation.values =@[(__bridgeid)[UIColorblueColor].CGColor,(__bridgeid)[UIColorredColor].CGColor,(__bridgeid)[UIColorgreenColor].CGColor,(__bridgeid)[UIColorblueColor].CGColor];

    

    [self.colorLayeraddAnimation:animation forKey:nil];

    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值