zoukankan      html  css  js  c++  java
  • Core Animation学习笔记三:CAkeyframeAnimation

    CAkeyframeAnimation:提供了关键帧动画的支持。你可以为层属性指定key path来使其产生动画,这个数组的值保存了动画每个阶段的值,同时还有key frame的次数和时间函数。在动画运行的时候,数组中的每个值就会被轮流进行插值使用。

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.title = [[self class] displayName];
        
        self.view.backgroundColor = [UIColor blackColor];
        
        CGFloat radius = 30.0f;
        CGFloat diameter = radius * 2;
        CGPoint arcCenter = CGPointMake(radius, radius);
        
        // Create a UIBezierPath for Pacman's open state
        pacmanOpenPath = [[UIBezierPath bezierPathWithArcCenter:arcCenter
                                                                   radius:radius 
                                                               startAngle:DEGREES_TO_RADIANS(35
                                                                 endAngle:DEGREES_TO_RADIANS(315)
                                                                clockwise:YES] retain];    


        [pacmanOpenPath addLineToPoint:arcCenter];
        [pacmanOpenPath closePath];
        
        // Create a UIBezierPath for Pacman's close state
        pacmanClosedPath = [[UIBezierPath bezierPathWithArcCenter:arcCenter
                                                                   radius:radius 
                                                               startAngle:DEGREES_TO_RADIANS(1
                                                                 endAngle:DEGREES_TO_RADIANS(359
                                                                clockwise:YES] retain];
        [pacmanClosedPath addLineToPoint:arcCenter];
        [pacmanClosedPath closePath];    
        
        // Create a CAShapeLayer for Pacman, fill with yellow
        shapeLayer = [CAShapeLayer layer];
        shapeLayer.fillColor = [UIColor yellowColor].CGColor;
        shapeLayer.path = pacmanClosedPath.CGPath;
        shapeLayer.strokeColor = [UIColor grayColor].CGColor;
        shapeLayer.lineWidth = 1.0f;
        shapeLayer.bounds = CGRectMake(00, diameter, diameter);
        shapeLayer.position = CGPointMake(-40, -100);
        [self.view.layer addSublayer:shapeLayer];
        
        SEL startSelector = @selector(startAnimation);
        UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:startSelector];
        [self.view addGestureRecognizer:recognizer];
        [recognizer release];
        
        // start animation after short delay
        [self performSelector:startSelector withObject:nil afterDelay:1.0];
    }

    - (void)startAnimation {

        // Create CHOMP CHOMP animation
        CABasicAnimation *chompAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
        chompAnimation.duration = 0.25;
        
        chompAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        chompAnimation.repeatCount = HUGE_VALF;
        chompAnimation.autoreverses = YES;
        // Animate between the two path values
        chompAnimation.fromValue = (id)pacmanClosedPath.CGPath;
        chompAnimation.toValue = (id)pacmanOpenPath.CGPath;
        
        [shapeLayer addAnimation:chompAnimation forKey:@"chompAnimation"];
        
        // Create digital '2'-shaped path
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(-40100)];
        [path addLineToPoint:CGPointMake(360100)];
        [path addLineToPoint:CGPointMake(360200)];
        [path addLineToPoint:CGPointMake(-40200)];
        [path addLineToPoint:CGPointMake(-40300)];
        [path addLineToPoint:CGPointMake(360300)];
        
        CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        moveAnimation.path = path.CGPath;
        moveAnimation.duration = 8.0f;
        // Setting the rotation mode ensures Pacman's mouth is always forward.  This is a very convenient CA feature.
        moveAnimation.rotationMode = kCAAnimationRotateAuto;
        [shapeLayer addAnimation:moveAnimation forKey:@"moveAnimation"];

  • 相关阅读:
    windows系统切换jdk,修改java_home无效情况
    Cannot instantiate interface org.springframework.context.ApplicationListener
    MySQL分组查询获取每个学生前n条分数记录(分组查询前n条记录)
    ASP.NET Web API 使用Swagger生成在线帮助测试文档,支持多个GET
    EF TO MYSQL 无法查询中文的解决方法
    HttpWebRequest post请求获取webservice void数据信息
    This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms. 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分 解决方案
    MySQL 5.7.13解压版安装记录 mysql无法启动教程
    C# udpclient 发送数据断网后自动连接的方法
    汽车XX网站秒杀抢购代码
  • 原文地址:https://www.cnblogs.com/bandy/p/2419281.html
Copyright © 2011-2022 走看看