CAShapeLayer的路径动画

时间:2013-09-24 05:14:10

标签: ios animation path cashapelayer

我画了四分之一圈。为此我用UIView中心作为中心。绘图部分很好。
现在我想在路径上制作动画。所以我使用了KeyFrame动画 问题是它在CAShapeLayer的中心旋转。我不想要。我想在边框周围旋转cgpath。看到初始位置,这是正确的。但是当开始在路径上旋转时,它将占据CAShapeLayer的中心,然后在路径上旋转。

以下是黄色季度的代码:

     CGMutablePathRef path = CGPathCreateMutable();
     CGPathAddArc(path, NULL, rect.size.width/2, rect.size.height/2, 100, (0), (M_PI_2), NO);
     CGPathAddArc(path, NULL, rect.size.width/2, rect.size.height/2, 100-50, (M_PI_2), (0), YES);
     CGPathCloseSubpath(path);
     CAShapeLayer* arcLayer = [[CAShapeLayer alloc]init];
     arcLayer.path = path;
     arcLayer.frame = CGPathGetPathBoundingBox(path);
     arcLayer.fillColor = [UIColor yellowColor].CGColor;
     arcLayer.backgroundColor = [UIColor whiteColor].CGColor;
     arcLayer.bounds = CGPathGetPathBoundingBox(path);

我在单击手势上盯着动画。这是动画的代码:

   - (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture{
     CGPoint touchPoint=[gesture locationInView:self];
     NSArray* subLayerArray = [pathLayer sublayers];
     for (int i = 0; i < subLayerArray.count; i++) {
     CAShapeLayer* layer = [subLayerArray objectAtIndex:i];
     if(CGPathContainsPoint(layer.path, NULL, touchPoint, NO)){
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathAddArc(path, NULL, self.bounds.size.width/2, self.bounds.size.height/2, 100, 0,  3*M_PI/2, NO);
        CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        anim.path = path;
        anim.rotationMode = kCAAnimationRotateAutoReverse;
        anim.fillMode = kCAFillModeForwards;
        anim.removedOnCompletion = NO;
        anim.duration = 100.0;
        [layer addAnimation:anim forKey:@""];
        }
     }
   }

Initial postion during rotaion

1 个答案:

答案 0 :(得分:0)

是的,我终于得到了解决方案。我们不要忘记layer的anchorPoint。我需要设置anchorPoint。arcLayer.anchorPoint = CGPointMake(0 , 1);。 希望这会对你有所帮助......