我有一个NSArray的字符串,我想用简单的淡入/淡出动画制作动画。
----------------------------------------------------------------------------------
| fade in
| String 1 ---> displayed in UILabel
| fade out
----------------------------------------------------------------------------------
接着是
----------------------------------------------------------------------------------
| fade in
| String 2
| fade out
----------------------------------------------------------------------------------
等...
每个人在继续到阵列中的下一个项目之前持有它的位置约5秒。我不知道从哪里开始...我知道如何执行动画但是我不太确定如何在每次迭代时使用5秒(非线程阻塞)暂停来设置字符串数组的动画。有什么想法吗?
根据接受的答案更新我的解决方案
- (void)startTimer {
arrayIndex = 0;
NSArray *myarray = [alerts_dict allValues];
breakingLabel.text = [myarray objectAtIndex:frameCount];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:1];
breakingLabel.alpha = 1;
[UIView commitAnimations];
arrayIndex++;
pauseTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(myFunction) userInfo:nil repeats:YES];
}
- (void)myFunction {
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^() {
breakingLabel.alpha = 0;
}
completion:^(BOOL finished) {
NSArray *myarray = [alerts_dict allValues];
if (arrayIndex == [myarray count])
arrayIndex = 0;
breakingLabel.text = [myarray objectAtIndex:arrayIndex];
arrayIndex++;
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^() {
breakingLabel.alpha = 1;
} completion:nil];
}];
}
答案 0 :(得分:1)
你可以使用块动画很容易地做到这一点:
alpha
降至0.0 text
设置为新字符串,并将其alpha
设置为1.0(或原始值,无论它是什么)。可以通过NSTimer
超时回调重复调用此例程。只需实例化一个间隔为5.0的计时器,指定回调并安排它在运行循环中运行,如下所示:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self
selector:@selector(timerFired:) userInfo:nil repeats:YES];
(...)
- (void)timerFired:(NSTimer *)timer
{
// call the method to update the label with the next string in the array
// or invalidate the timer if you've reached the end of array
}
答案 1 :(得分:1)
我会这样做:
在viewDidLoad中将字符串的alpha设置为0:使用这段代码
for (UILabel *label in labelArray) {
label.alpha = 0;
}
然后在viewWillAppear:或viewDidAppear中:放置此代码(取决于您想要的内容,希望它在视图出现后立即启动)
for (int i = 0; i < 5; i++) {
[self animateView:[labelArray objectAtIndex:i] withDelay:i * 5];
}
然后该方法的实现将类似于:
- (void)animateView:(UIView *)view withDelay:(float)delay {
[UIView animateWithDuration:1 delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:^{
view.alpha = 1.0;
} completion:^(BOOL completed) {
[UIView animateWithDuration:1 animations:^{
view.alpha = 0.0];
}];
}];
}
当然,您可以调整此方法以满足您的需求(例如,如果您想使其更通用,请在其中添加持续时间参数)。