2013-01-22 19 views
2

我被困在动画uiLabel在我看来。如何在iOS中的循环路径上动画标签?

其实我只想在圆形路径上为我的标签/文字制作动画。我在互联网上搜索了很多,但找不到任何好的教程或例子,正在做我的问题。我已经完成了CAShapeLayer(圆圈)从0增加到其最大值......

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

希望我已经相当准确地解释了我的问题......仍然有任何遗漏或无法理解的地方。请问我。

感谢您的期待。

+0

你学会了如何做基本的旋转? – JoshRagem

回答

8

IOMI嗨,我只是谷歌,我发现最好的问题类似像你This

娄它一斤的回答希望它可以帮助你

 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

这里是DEMO LINK

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

+0

谢谢尼廷,它真的解决了我的问题。当我复制上面编写的代码时,我只遇到一个小问题,它显示Label为黑色块,但是当我从demo链接下载代码时。它真的很好....再次非常感谢我的倾诉。 – iOmi

+0

欢迎iomi在asnwer代码我忘了把'self.view.bounds' –

+0

::我想问一个更多的请。与'CA​​BasicAnimation'一样,我可以通过改变'From value'和'Value'的值来设置旋转的顺时针和逆时针方向。'CAKeyFrameAnimation'中有任何选项使其顺时针或逆时针旋转。 – iOmi