繁体   English   中英

iOS:简单的动画

[英]iOS: simple animation

我想创建一个更改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 - > ...依此类推,持续时间为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];
}

在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:

您创建如下所示的ViewController

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

然后添加动画,但是什么时候在视图heirarchy(addSubview)中添加firstViewController.view。 也只是打电话

[[[FirstViewController alloc] init]autorelease]

除非您覆盖其init方法,否则不会创建标签对象。 如果在该时间点分配了标签,请尝试调试。 同样对于动画我使用uiview anitmation和完成块方法

我想,你可以只使用一个功能(基于以前的答案的功能):

- (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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM