我最近只是在玩 CA。现在我有点卡住了。这是我想要动画的东西:
至于现在我已经让圆形动画工作了。我将 CALayer 子类化来制作动画。我真的不知道从这里去哪里。我必须在哪里添加 CATextLayer 的子层?如何同时为两者设置动画,使其看起来像带有线条的文本粘在圆圈末端?
如果您需要一些代码或其他任何内容,请告诉我。
我真的很乐意在这里得到一些帮助:-)
非常感谢!
我最近只是在玩 CA。现在我有点卡住了。这是我想要动画的东西:
至于现在我已经让圆形动画工作了。我将 CALayer 子类化来制作动画。我真的不知道从这里去哪里。我必须在哪里添加 CATextLayer 的子层?如何同时为两者设置动画,使其看起来像带有线条的文本粘在圆圈末端?
如果您需要一些代码或其他任何内容,请告诉我。
我真的很乐意在这里得到一些帮助:-)
非常感谢!
我能够做到你在下面要求的。代码很粗糙,只花了几分钟,可能错过了发布,你有什么。您可以简单地将 UILabel 换成 CATextLayer(为简洁起见,使用 UILabel)。
CAShapeLayer* circle = [[CAShapeLayer alloc] init];
CGMutablePathRef path = CGPathCreateMutable();
CGRect textRect = CGRectMake(self.bounds.size.width/4, (self.bounds.size.height-self.bounds.size.width/2)/2, self.bounds.size.width/2, self.bounds.size.width/2);
float midX = CGRectGetMidX(textRect);
float midY = CGRectGetMidY(textRect);
CGAffineTransform t = CGAffineTransformConcat(
CGAffineTransformConcat(
CGAffineTransformMakeTranslation(-midX, -midY),
CGAffineTransformMakeRotation(-1.57079633/0.99)),
CGAffineTransformMakeTranslation(midX, midY));
CGPathAddEllipseInRect(path, &t, textRect);
circle.path = path;
circle.frame = self.bounds;
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor blackColor].CGColor;
circle.lineWidth = 60.0f;
[self.layer addSublayer:circle];
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.duration = 15.0f;
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat:1.0f];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[circle addAnimation:animation forKey:@"strokeEnd"];
[circle release];
UILabel* label = [[UILabel alloc] init];
label.text = @"Test Text";
label.font = [UIFont systemFontOfSize:20.0f];
label.center = CGPathGetCurrentPoint(path);
label.transform = CGAffineTransformMakeRotation(1.57079633);
[label sizeToFit];
[self.layer addSublayer:label.layer];
CAKeyframeAnimation* textAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
textAnimation.duration = 15.0f;
textAnimation.path = path;
textAnimation.rotationMode = kCAAnimationRotateAuto;
textAnimation.calculationMode = kCAAnimationCubicPaced;
textAnimation.removedOnCompletion = NO;
[label.layer addAnimation:textAnimation forKey:@"position"];
我建议创建一个新的 UIView 类,然后使用方法 layoutSubviews。
有关 UIView 类的更多信息,请查看类参考。
只需在您的主视图中使用以下内容:
myView = [[myView alloc] initWithFrame:CGRectMake(100,100,100,100)];
[self.view addSubview:myView];
对于 layoutSubviews 来说是这样的:
- (void) layoutSubviews{
[super layoutSubviews];
mainLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
CALayer *circle = [[[CALayer alloc] init] autorelease];
//You have to set up the circle image and everything
circle.position = mainLayer.position;
}
当然,您必须在 .h 中添加 myView 的实现,否则其他一切都应该工作。
编辑:对不起,我忘记将文本图层代码添加到 layoutSubview,但您要做的就是创建图层并将其位置设置为 mainLayer.position;