2011-06-17 86 views
7

我想创建一个简单的动画改变alpha值:iOS的:简单的动画

[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 - > ...等等,持续时间为2秒。 你能帮我吗?

我的完整代码

-(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]; 
} 

回答

6
- (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]; 
+0

我会试试..... – CrazyDev 2011-06-17 10:46:35

+1

如果我想停止这个动画? – CrazyDev 2011-06-17 10:47:18

3

您可以设置动画完成处理程序(使用基于块的API或+(void)setAnimationDidStopSelector:(SEL)selector),并开始下一个。

或者看看那些方法:

+ setAnimationRepeatCount: 
+ setAnimationRepeatAutoreverses: 
+0

+1提及重复和autoreverses。 – 2011-06-17 10:44:42

0

我想,你可以只使用一个函数(根据以前的答案fu nctions):

- (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不能为负值,因此国防部必须应用

10

在iOS 4的,以后你可以做

view1.alpha = 1.0; 
    [UIView animateWithDuration:2.0 
          delay:0.0 
         options:UIViewAnimationOptionRepeat|UIViewAnimationAutoReverse 
        animations:^{ 
         view1.alpha = 0.0; 
        } 
        completion:nil 
    ]; 
0

您创建一个视图控制器像下面

FirstViewController *firstViewController = [[[FirstViewController alloc] init]autorelease]; 

然后向它添加动画,但是何时在视图中添加firstViewController.view heirarchy(addSub视图)。也只是打电话

[[[FirstViewController alloc] init]autorelease] 

将不会创建标签对象,除非您已覆盖其init方法。如果标签在该时间点分配,请尝试调试。 也为动画我使用uiview anitmation与完成块方法