• 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"];

  • 相关阅读:
    超级管理员登录后如果连续XX分钟没有操作再次操作需要重新登录
    linux下连接本地的navicate
    linux 日常中会用到的命令(持续更新)
    关于PHP的 PHP-FPM进程CPU 100%的一些原因分析和解决方案
    VMware Workstation 14 激活码
    破解phpstorm
    redis五种数据类型的使用场景
    关于Nginx负载均衡的6种策略
    PHP和PHP-FPM 配置文件优化
    redis配置文件详解
  • 原文地址:https://www.cnblogs.com/bandy/p/2419281.html
Copyright © 2020-2023  润新知