简体   繁体   English

iOS:简单的动画

[英]iOS: simple animation

I want to create a simple animation changing alpha value: 我想创建一个更改alpha值的简单动画:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[view1 setAlpha:0.00];
[UIView commitAnimations];

Ok in this way I change the alpha value for two seconds and it works fine, but I want this thing: When alpha is 0 the animation must should start another time to alpha = 1 So the animation should be: alpha 0 -> alpha 1 -> alpha 0 -> alpha 1 -> ... and so on with a duration of two seconds. 好的,这样我将alpha值更改为两秒并且它工作正常,但我想要这样的事情:当alpha为0时,动画必须再次开始alpha = 1所以动画应该是:alpha 0 - > alpha 1 - > alpha 0 - > alpha 1 - > ...依此类推,持续时间为2秒。 Can you help me? 你能帮助我吗?

my complete code is 我的完整代码是

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

In iOS 4 and later you can do 在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];
}

These will call each other on the end of the animation... 这些将在动画结束时互相调用......

All you need to do to start the ball rolling is call: 你需要做的就是打电话:

[self fadeOut:nil finished:nil context:nil];

You can set a animation completion handler (either using the block-based API or +(void)setAnimationDidStopSelector:(SEL)selector ) and start the next one. 您可以设置动画完成处理程序(使用基于块的API或+(void)setAnimationDidStopSelector:(SEL)selector )并启动下一个。

Alternatively have a look at those to methods: 或者看看那些方法:

+ setAnimationRepeatCount:
+ setAnimationRepeatAutoreverses:

You create a ViewController like below 您创建如下所示的ViewController

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

And then adding animation to it, but when did you add firstViewController.view in view heirarchy(addSubview). 然后添加动画,但是什么时候在视图heirarchy(addSubview)中添加firstViewController.view。 Also just calling 也只是打电话

[[[FirstViewController alloc] init]autorelease]

wont create label object unless you have override its init method. 除非您覆盖其init方法,否则不会创建标签对象。 Try debugging if label is allocated at that point of time. 如果在该时间点分配了标签,请尝试调试。 Also for animation i use uiview anitmation with completion block method 同样对于动画我使用uiview anitmation和完成块方法

I think, you can use just one function (based on previous answer's functions): 我想,你可以只使用一个功能(基于以前的答案的功能):

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

The alplha must not be a negative value, so a mod must be applied alplha不能是负值,因此必须应用mod

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

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