我想创建一个简单的动画更改alpha值:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[view1 setAlpha:0.00];
[UIView commitAnimations];
好的,通过这种方式,我改变阿尔法值两秒钟,它很好,但我想要这个:当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];
}
在iOS 4及更高版本中,您可以执行
view1.alpha = 1.0;
[UIView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionRepeat|UIViewAnimationAutoReverse
animations:^{
view1.alpha = 0.0;
}
completion:nil
];
- (void) fadeOut:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
if(animationRunning){
[UIView setAnimationDidStopSelector:@selector(fadeIn:finished:context:) ];
}
[view1 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];
if(animationRunning){
[UIView setAnimationDidStopSelector:@selector(fadeOut:finished:context:) ];
}
[view1 setAlpha:1.00];
[UIView commitAnimations];
}
这些将在动画结束时相互调用。。。
你所需要做的就是调用:
[self fadeOut:nil finished:nil context:nil];
您可以设置动画完成处理程序(使用基于块的API或+(void)setAnimationDidStopSelector:(SEL)selector
)并启动下一个处理程序。
或者看看这些方法:
+ setAnimationRepeatCount:
+ setAnimationRepeatAutoreverses:
我认为,您可以只使用一个函数(基于以前答案的函数):
- (void) fadeIn:(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:) ];
[view1 setAlpha:mod(1-view1.alpha)];
[UIView commitAnimations];
}
alplha不能是负值,因此必须应用mod
您创建了一个类似的ViewController
FirstViewController *firstViewController = [[[FirstViewController alloc] init]autorelease];
然后添加动画,但您是何时在视图继承(addSubview)中添加firstViewController.view的。也只是调用
[[[FirstViewController alloc] init]autorelease]
不会创建标签对象,除非您覆盖了它的init方法。如果在该时间点分配了标签,请尝试调试。同样对于动画,我使用uiview动画和完成块方法