我想创建一个简单的动画,改变 alpha 值:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[view1 setAlpha:0.00];
[UIView commitAnimations];
好的,我这样更改alpha值两秒钟,它可以正常工作,但我想要这个: 当alpha为0时,动画必须再次开始,直到alpha = 1。 因此,动画应该是:alpha 0 -> alpha 1 -> alpha 0 -> alpha 1 -> ...,以两秒的持续时间循环进行。 你能帮我吗?
我的完整代码如下:
-(IBAction){
FirstViewController *firstViewController = [[[FirstViewController alloc] init]autorelease];
[firstViewController.label1 setAlpha:1.00];
[firstViewController.label2 setAlpha:1.00];
view = firstViewController.viewFirst; //this is my view and I copy in other view
[self fadeOut:nil finished:nil context:nil];}
- (void) fadeOut:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(fadeIn:finished:context:) ];
[view setAlpha:0.00];
[UIView commitAnimations];
}
- (void) fadeIn:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(fadeOut:finished:context:) ];
[view setAlpha:1.00];
[UIView commitAnimations];
}
BOOL animationRunning
,并在[UIView setAnimationDidStopSelector:@selector(...)]
这一行周围放置if(animationRunning){ ... }
。然后当你想要停止动画时,只需设置animationRunning = NO
(当你想要运行动画时反之亦然)。 - Thomas Clayson