gpt4 book ai didi

objective-c - iOS 10 UILabels -> 1 UIView -> 使用动画循环

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:20:32 25 4
gpt4 key购买 nike

我有 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,就像一个小部件。

最佳答案

使用 NSTimer 而不是循环进行编辑。

counter 是在 header 中定义的 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;
}
}

关于objective-c - iOS 10 UILabels -> 1 UIView -> 使用动画循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7840261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com