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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM