【问题标题】:iOS 10 UILabels -> 1 UIView -> Loop through with AnimationiOS 10 UILabels -> 1 UIView -> 循环动画
【发布时间】:2011-10-20 18:14:14
【问题描述】:

我有 10 个UILabels,也许还有一些UIImageViews。我想在 1 UIView 中显示所有这些,并一个接一个地平滑 FadeOut 和 FadeIn 过渡。

我知道将 UIView 动画放在 for 循环中是行不通的,因为动画是异步完成的,并且不会产生适当的效果。所以通常我会这样做的方式是将UIView动画链接在一起。即在一个元素动画完成后开始下一个。对于 3-4 个元素,代码看起来没问题。像这样 -

[UIView animateWithDuration:0.25 
                      delay:0 
                    options:UIViewAnimationCurveEaseInOut 
                 animations:^{ //do something with alpha here - first element } 
                 completion:^(BOOL finished){ 
                     [UIView animateWithDuration:0.25 
                             delay:0 
                             options:UIViewAnimationCurveEaseInOut 
                             animations:^{ //do something with alpha here - 2nd element} 
                                      completion:^(BOOL finished){ ... }

                 }

但是对于 10 多个元素,它会变得非常混乱。怎么做呢?本质上,我正在创建一个带有循环内容的UIView,就像一个小部件。

【问题讨论】:

    标签: objective-c ios animation uiview core-animation


    【解决方案1】:

    使用NSTimer 而不是循环编辑。

    counter 是在标头中定义的 ivar。

     - (void)viewDidLoad
    {
        [super viewDidLoad];
        counter = 0;
        [NSTimer scheduledTimerWithTimeInterval:0.30
                                         target:self
                                       selector:@selector(timerTick:)
                                       userInfo:nil
                                        repeats:YES];
    }
    
    - (void) timerTick:(NSTimer *)timer{
    
        UIView *currentView = [self.view.subviews objectAtIndex:counter];
        [UIView animateWithDuration:0.25 
                              delay:0 
                            options:UIViewAnimationCurveEaseInOut 
                         animations:^{ currentView.alpha = 1.0;}
                         completion:^(BOOL finished){
                             [UIView animateWithDuration:0.25 animations:^{currentView.alpha = 0.0;}];
                         }
        ];
        counter++;
        if (counter >= [self.view.subviews count]) {
            counter = 0;
        }
    }
    

    【讨论】:

    • 这将在i < [self.View.Subviews count] 之后停止。如何让它无限循环?
    • 我想我误解了你。所以你想让这无限重复吗?
    • 啊我明白了。使用 NSTimer 而不是循环。我将发布一个示例代码。
    • 我有一些 10 UILabels 和一些 5 UIImageViews 我希望他们继续一个接一个地无限循环,或者直到一个新的数据集到达......所以我的问题是 1.how继续无限循环? 2.如果我想刷新数据,怎么做?我的意思是动画可能正在进行对吗?这清楚吗?
    • 实际上,这应该是动态的。如果你在没有 ARC 的情况下工作,你应该保留 currentView 并在 timerTick: 方法中释放它,以防它从他的超级视图中删除。
    猜你喜欢
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多