核心动画循环无间隙

时间:2014-07-03 11:37:39

标签: ios objective-c core-animation cabasicanimation

enter image description here

假设我为中央橙色制作动画" HELLO"使用下面的代码从左到右

问:如何为下面的自动收录器设置动画,使其看起来像视图" HELLO"没有间隙或跳跃,无休止地移动。 (想象它就像股票市场的股票代码......)

-(void)animate:(UIView *)thisView
    {
        CABasicAnimation *moveView;
        moveView            = [CABasicAnimation animationWithKeyPath:@"position.x"];
        moveView.byValue    = @(self.view.frame.size.width);
        moveView.duration   = 3.0;
        moveView.removedOnCompletion = YES;
        moveView.fillMode   = kCAFillModeRemoved;
        moveView.autoreverses = NO;
         moveView.repeatCount = 10;
        [[thisView layer] addAnimation:moveView forKey:@"x"];
 }

1 个答案:

答案 0 :(得分:2)

好的,要做到这一点,你需要两个标签。另外,我将使用基于块的动画。您使用的动画方法非常陈旧,因此不鼓励iOS4。

<强>总之...

首先,您需要像这样设置您的视图...

               | Screen width  |
               |               |
[    Label    ]|[    Label    ]|

这两个标签应该在一个视图中,它是屏幕宽度的两倍。

所以containerView的框架会开始像......

CGRect containerFrame = CGRectMake(-320, 0, 640, 44);

然后你可以为动画做到这一点......

- (void)animateLabels
{
    [UIView animateWithDuration:3.0
                          delay:0.0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         // animate the container frame to the right 320 points
                         containerView.frame = CGRectOffset(containerView.frame, 320, 0);
                     }
                     completion:^(BOOL finished) {
                         // move it back to where it started
                         containerView.frame = CGRectOffset(containerView.frame, -320, 0);
                         [self animateLabels];
                     }];
}

这会将两个标签滑动到右侧,然后快速将它们放回原点并再次开始动画。它会给人一种在屏幕上滑动无限标签的错觉。