在UIView中心周围旋转CAShapeLayer

时间:2013-09-23 04:27:47

标签: ios animation cashapelayer

我正在研究CAShapeLayer的动画。我正在绘制第一象限的四分之一圆(参考图像)(据我所知)。现在我想将它旋转90度所以它应该在第二象限中修复动画在单击手势上。我尝试了但它没有固定到第二象限。我想绕着UIView的中心旋转。

单击手势:

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture {
    CGPoint touchPoint=[gesture locationInView:self];
    NSArray* subLayerArray = [self.layer 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-layer.frame.size.width/2, self.bounds.size.height/2-layer.frame.size.height/2, 100, 0*M_PI, M_PI, YES);            
            CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
            anim.path = path;
            anim.rotationMode = kCAAnimationRotateAuto;
            anim.calculationMode = kCAAnimationPaced;
            anim.fillMode = kCAFillModeForwards;
            anim.removedOnCompletion = NO;
            anim.duration = 10.0;
            [layer addAnimation:anim forKey:@""];
        }
    }
}

绘图代码:

- (void)drawRect:(CGRect)rect {
    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.bounds = CGPathGetPathBoundingBox(path);
    [self.layer addSublayer:arcLayer]; 
}

和图片:

enter image description here

0 个答案:

没有答案