Rotate CAShapeLayer around center of UIView

谁说胖子不能爱 提交于 2019-12-11 19:26:45

问题


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:

来源:https://stackoverflow.com/questions/18951781/rotate-cashapelayer-around-center-of-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!