ios - 如何在 iOS 中为圆形路径上的标签设置动画?

标签 ios objective-c cabasicanimation catransformlayer

我无法在我的 View 中为 uiLabel 设置动画。

实际上,我只想在圆形路径上为我的标签/文本设置动画。我在互联网上搜索了很多,但找不到任何好的教程或示例来解决我的问题。我完成了 CAShapeLayer(圆圈)从 0 增加到最大值....

同样,我希望我的标签/文本从圆形路径的初始点开始移动,并在完成一圈后回到初始点。

希望我已经非常准确地解释了我的问题……如果有任何遗漏或无法理解的地方。请问我。

感谢期待。

最佳答案

嗨,iOmi,我刚刚用谷歌搜索了一下,我发现 Best Question 与您的 This 相似

下面是一个jin的回答 希望对你有帮助

        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"];

我刚刚创建了一个演示,屏幕截图是:-

enter image description here

这是演示链接

http://www.sendspace.com/file/dq5i1e

关于ios - 如何在 iOS 中为圆形路径上的标签设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14452086/

相关文章:

ios - 如何在 ios 中获取动画 uiview 的当前位置

iphone - 代码可以在模拟器上运行,但不能在 iPhone 上运行?

ios - Cocoapods 页面重定向到 github

objective-c - NsTask执行终端命令的问题

iphone - 无法创建持久存储协调器

iphone - 当应用程序进入和退出后台时暂停和恢复核心动画

ios - CAAnimationGroup 中的 CAAnimation 委托(delegate)

iphone - 在 iOS 上将属性从一个 Controller 传递到另一个 Controller

ios - Storyboard减少 View 之间的差距

iphone - 基本保留,自动释放问题