iOS - 仅为UIBezierPath的'appendPath'设置动画

时间:2014-03-01 17:39:51

标签: ios animation uibezierpath cashapelayer

有很多例子说明如何在UIBezierPath上进行动画处理。但我无法弄明白,如何仅动画UIBezierPath的appendPath。

在我的情况下,我有以下示例:

// Create path from view controller
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:
                      CGRectMake(0, 0, tutorialController.bounds.size.width,
                                 tutorialController.bounds.size.height) cornerRadius:0];

// Create circle path
UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x, y, 2.0*radius, 2.0*radius) cornerRadius:radius];
[path appendPath:circlePath];
[path setUsesEvenOddFillRule:YES];

// Create spot layer
spotLayer = [CAShapeLayer layer];
spotLayer.path = path.CGPath;
spotLayer.fillRule = kCAFillRuleEvenOdd;
spotLayer.fillColor = [UIColor blackColor].CGColor;
spotLayer.opacity = 0.90;

所以基本上它是一个矩形内的一个圆圈,我现在想要只为圆形动画。在所有尝试中,rect也被移动了。

这是我的例子,我如何能够“沿着”路径动画

CGPoint pathStart = CGPointMake(96.000000, 226.000000);
CGPoint pathEnd = CGPointMake(120.000000, 300.000000);

[circlePath moveToPoint:pathStart];
[circlePath addLineToPoint:pathEnd];

CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.path = circlePath.CGPath;
anim.duration = 5.0;
anim.calculationMode = kCAAnimationPaced;
[spotLayer addAnimation:anim forKey:@"bezierPathAnim"];

但这不是我想要的...... 我想要做的是,通过动画

移动我的圆形路径(这是一个矩形路径的附加路径)

有人有想法吗?

此致

1 个答案:

答案 0 :(得分:0)

我解决了我的问题: - )

这是解决方案......

// If there is an old path animate to the new path
if(oldPath != nil){
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"path"];
    [anim setFromValue:(__bridge id)oldPath];
    [anim setToValue:(__bridge id)[path CGPath]];
    [anim setDuration:0.3];
    [anim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [spotLayer addAnimation:anim forKey:@"path"];
}

首先,我保留了oldPath(起点),然后设置为新设置路径的动画。