我最近只是在和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"];
关于iphone - CoreAnimation CALayer和CATextLayer的组合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7947529/