• 关键帧动画(2)花瓣


    #import "ViewController.h"

     

    @interface ViewController ()

    {

        CALayer *petalLayer;

    }

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];

        imageView.image = [UIImage imageNamed:@"落叶.jpg"];

        [self.view addSubview:imageView];

        

        [self addPetal];

    }

    - (void)addPetal

    {

        UIImage *petal = [UIImage imageNamed:@"petal.jpg"];

        

        petalLayer = [[CALayer alloc]init];

        petalLayer.bounds = CGRectMake(0, 0, petal.size.width, petal.size.height);

        petalLayer.position = CGPointMake(100, 200);

        petalLayer.contents = (id)petal.CGImage;

        [self.view.layer addSublayer:petalLayer];

    }

     

    - (void)addAnimation {

        CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

        keyAnimation.duration = 5;

        

    //    创建路径

        CGMutablePathRef path = CGPathCreateMutable();

    //    给路径添加一个 起始点

        CGPathMoveToPoint(path, NULL, petalLayer.position.x, petalLayer.position.y);

    //    有起始点 可以通过起始点 到 另一个点 画一条线

        CGPathAddLineToPoint(path, NULL, petalLayer.position.x+100, petalLayer.position.y+100);

         CGPathAddLineToPoint(path, NULL, petalLayer.position.x-100, petalLayer.position.y+300);

       

    //    关闭路径

    //    CGPathCloseSubpath(path);

    //    将路径添加到 keyAnimation(动画上)

        keyAnimation.path = path;

        

    //    释放路径

    //    但凡通过quarzt2d中带有creat/copy/retain方法创建出来的值都必须手动的释放

    //    有两种方法可以释放前面创建的路径:

    //    (1)CGPathRelease(path);

    //    (2)CFRelease(path);

    //    说明:CFRelease属于更底层的cocafoundation框架

     

        CGPathRelease(path);

        path = nil;

        

        

        keyAnimation.removedOnCompletion = NO;

        keyAnimation.fillMode = kCAFillModeBoth;

        keyAnimation.repeatCount = 10;

        

        

        [petalLayer addAnimation:keyAnimation forKey:@"show"];

    }

     

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

        [self addAnimation];

    }

     

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    @end

     

  • 相关阅读:
    RabbitMQ In JAVA 介绍及使用
    JSON Web Token 入门教程
    char* 与 char[] 的区别
    C++ namespace的用法
    启动其他APK的Activity方法 (转至http://www.cnblogs.com/lijunamneg/archive/2013/02/26/2934060.html)
    Android多任务切换与Activity启动模式SingleTask之间关系的分析
    对SingleTask和TaskAffinity的理解(转至 http://www.2cto.com/kf/201311/254450.html)
    正在运行的android程序,按home键之后退回到桌面,在次点击程序图标避免再次重新启动程序解决办法
    大牛的博客
    Androidpn 简单实现及分析
  • 原文地址:https://www.cnblogs.com/wukun16/p/4884169.html
Copyright © 2020-2023  润新知