首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何循环UIAnimation?

如何循环UIAnimation?
EN

Stack Overflow用户
提问于 2011-11-29 13:45:21
回答 1查看 3K关注 0票数 0

我的动画播放得很好。但在动画结束时,动画就停止了。我要动画循环。我该怎么做?这是我的代码:

代码语言:javascript
复制
    - (void) startTicker
{
    if (self.timerIsRunning) return;

    self.timerIsRunning = YES;

    NSTimer *newTimer = [NSTimer timerWithTimeInterval:(0.1) target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:newTimer forMode:NSDefaultRunLoopMode];

   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationDuration:100.0];
   [UIView setAnimationDelegate:self];
   [UIView setAnimationDelay:0.0];
   [UIView setAnimationDidStopSelector:@selector(moveToLeft:finished:context:)];
   [UIView commitAnimations];


}


     -(void)moveToLeft:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
    { 

        imageView.left = 800; 
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:100.0];
        [UIView setAnimationDelay:0.0];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self cache:YES];
        imageView.right = 800;

        [UIView setAnimationDidStopSelector:@selector(moveToLeft2:finished2:context2:)];
        [UIView commitAnimations];



    }
EN

回答 1

Stack Overflow用户

发布于 2013-09-01 21:13:57

对于循环动画,最好的方法是使用块动画,这是UIView的一部分。

调用该方法时:

代码语言:javascript
复制
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

您可能需要的“选项”是UIViewAnimationOptionAutoreverse,并与UIViewAnimationOptionRepeat相结合。

例如:(循环闪烁红色边框)

代码语言:javascript
复制
[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
    [redBorder setAlpha:0]; //first part of animation
    [redBorder setAlpha:0.5]; //second part of animation
} completion:nil];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8311764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档