我正在研究 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];
}
和图像: