1

我画了四分之一圆。为此,我使用 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:@""];
        }
     }
   }

初始位置 旋转期间

4

1 回答 1

0

是的,最后我得到了解决方案。我们不要忘记图层的锚点。这里我需要设置锚点。arcLayer.anchorPoint = CGPointMake(0 , 1);. 希望对你有帮助...

于 2013-09-24T09:26:00.390 回答