iOS:简单动画

7

我想创建一个简单的动画,改变 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];
}
5个回答

10

iOS 4及更高版本中,你可以执行以下操作:

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

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

1
如果我想停止这个动画怎么办? - cyclingIsBetter
警告:语义问题:找不到方法“-fadeOut:finished:context:”(返回类型默认为“id”)为什么? - cyclingIsBetter
如果你想停止动画,我会有一个实例变量(全局类变量)BOOL animationRunning,并在[UIView setAnimationDidStopSelector:@selector(...)]这一行周围放置if(animationRunning){ ... }。然后当你想要停止动画时,只需设置animationRunning = NO(当你想要运行动画时反之亦然)。 - Thomas Clayson
好的,你能把整个类发布出来吗?包括我给你的方法。 - Thomas Clayson
一切都没问题,我没有在 IB 中链接视图。 - cyclingIsBetter
显示剩余2条评论

3

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

另外,还可以查看这两个方法:

+ setAnimationRepeatCount:
+ setAnimationRepeatAutoreverses:

0

我认为,你可以只使用一个函数(基于之前答案的函数):

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

Alpha值不能为负数,因此必须应用模运算。


0
你可以像下面这样创建一个 ViewController:
FirstViewController *firstViewController = [[[FirstViewController alloc] init]autorelease];

然后将动画添加到其中,但是您何时在视图层次结构(addSubview)中添加了firstViewController.view?同时只需调用

[[[FirstViewController alloc] init]autorelease]

如果您没有重写其init方法,则不会创建标签对象。如果在那个时间点分配了标签,请尝试调试。 此外,对于动画,我使用带有完成块方法的uiview动画。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接