Rotate CAShapeLayer around center of UIView

2019-08-29 04:01发布

问题:

I am working on animation of CAShapeLayer. I am drawing quarter circle(refer image) which is in 1st quadrant(as per my knowledge). Now I want to rotate it 90 degree so it should fix in 2nd quadrant with animation on single tap gesture. I tried it but it is not fixing into 2nd quadrant. I want to rotate around center of UIView.

Single tap gesture:

- (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:@""];
        }
    }
}

Drawing Code:

- (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]; 
}

and Image: