如何在iOS中为循环路径上的标签设置动画?
我被困在我的视图中的uiLabel动画中 实际上,我只想在一个循环路径上设置标签/文本的动画。我在网上搜索了很多,但找不到任何好的教程或例子来解决我的问题。我已经完成了CAShapeLayer(圆)从0增加到其最大值 类似地,我希望我的标签/文本开始从循环路径的初始点移动,并在完成一个循环后返回其初始点 希望我已经非常准确地解释了我的问题。。。如果有什么遗漏或不可理解的地方。请问我如何在iOS中为循环路径上的标签设置动画?,ios,objective-c,cabasicanimation,catransformlayer,Ios,Objective C,Cabasicanimation,Catransformlayer,我被困在我的视图中的uiLabel动画中 实际上,我只想在一个循环路径上设置标签/文本的动画。我在网上搜索了很多,但找不到任何好的教程或例子来解决我的问题。我已经完成了CAShapeLayer(圆)从0增加到其最大值 类似地,我希望我的标签/文本开始从循环路径的初始点移动,并在完成一个循环后返回其初始点 希望我已经非常准确地解释了我的问题。。。如果有什么遗漏或不可理解的地方。请问我 谢谢你的期待。嗨,iOmi,我刚用谷歌搜索了一下,我发现最好的问题和你的相似 贝娄,这是阿金的回答,希望对你有帮助
谢谢你的期待。嗨,iOmi,我刚用谷歌搜索了一下,我发现最好的问题和你的相似 贝娄,这是阿金的回答,希望对你有帮助
CAShapeLayer* circle = [[CAShapeLayer alloc] init];
CGMutablePathRef path = CGPathCreateMutable();
CGRect textRect = CGRectMake(self.view.bounds.size.width/4, (self.view.bounds.size.height-self.view.bounds.size.width/2)/2, self.view.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"];
我刚刚创建了一个演示,屏幕截图是:-
这里是演示链接
你学会了如何进行基本的旋转吗?谢谢尼廷,它真的解决了我的问题。我面临的一个小问题是,当我复制上面编写的代码时,标签显示为黑色块,但当我从demo链接下载代码时。它真的很管用。。。。再次感谢您的关注。您欢迎iomi加入asnwer code我忘了放一个
self.view.bounds
::我想再问一件事。正如在CABasicAnimation
中一样,我可以通过将从value
和更改为value
来设置顺时针和逆时针旋转方向在cakeyKeyLeanimation
中是否有任何选项使其顺时针或逆时针旋转。