CAKeyFrameAnimation 的简单用法

来源:互联网 发布:奶茶网络营销策划方案 编辑:程序博客网 时间:2024/05/18 21:10

这里写图片描述

CAKeyFrameAnimation: 我们可以用 CGPath 来执行动画的轨迹

- (void)positionLayer {    //创建路径    UIGraphicsBeginImageContext(self.view.bounds.size);    CGContextRef contextRef = UIGraphicsGetCurrentContext();    CGMutablePathRef path = CGPathCreateMutable();    CGAffineTransform  transform = CGAffineTransformIdentity;    CGPathMoveToPoint(path, &transform, 200, 199);    CGPathAddArc(path, &transform, 150, 199, 50, 0, M_PI*2, NO);    CGContextAddPath(contextRef, path);    [[UIColor redColor]setStroke];    CGContextSetLineWidth(contextRef, 3);    CGContextDrawPath(contextRef, kCGPathStroke);    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();    UIImageView * imageView = [[UIImageView alloc]initWithImage:image];    [self.view addSubview:imageView];    UIGraphicsEndImageContext();    //初始化positionLayer    CALayer * positionLayer = [[CALayer alloc]init];    positionLayer.backgroundColor = [UIColor blueColor].CGColor;    positionLayer.frame = CGRectMake(0, 100, 10, 10);    positionLayer.cornerRadius = 3;    [self.view.layer addSublayer:positionLayer];    CAKeyframeAnimation * keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];    keyFrameAnimation.removedOnCompletion = NO;    keyFrameAnimation.fillMode = kCAFillModeForwards;    keyFrameAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];    keyFrameAnimation.autoreverses = NO;    keyFrameAnimation.duration = 2;    keyFrameAnimation.repeatCount = MAXFLOAT;    keyFrameAnimation.path = path;    [positionLayer addAnimation:keyFrameAnimation forKey:nil];    CGPathRelease(path);}
0 0