如何在iOS中的圆形路径上动画标签?

时间:2013-01-22 05:24:10

标签: ios objective-c cabasicanimation catransformlayer

在我看来,我被困在动画uiLabel。

实际上我只想在圆形路径上设置标签/文本的动画。我已经在互联网上搜索了很多但是找不到任何好的教程或例子来做我喜欢的问题。我已经完成了CAShapeLayer(circle)从0增加到它的最大值....

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

希望我已经非常准确地解释了我的问题......如果有任何遗漏或不可理解的话。请问我。

感谢您的期待。

1 个答案:

答案 0 :(得分:8)

嗨iOmi我只是google它,我发现最类问题类似于你的This

Bellow这是一个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