簡體   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