2010-11-09 60 views
1

莫斯科晚上給大家!我還是不熟悉iPhone動畫的原理(順便說一句,有沒有人知道一個關於這個大而好的教程?),但在我的項目中,我想做的按鈕「突出顯示,不突出顯示」閃爍通知用戶它的標籤已經改變。動畫選擇UIButton

此代碼不會做任何事情(它只是閃爍的動畫片段):

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration: 0.5]; 

[button setHighlighted: YES]; 

[UIView commitAnimations]; 

而這其中凸顯的按鈕,但不這樣做的動畫形式:

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration: 0.5]; 

[button setSelected: YES]; 

[UIView commitAnimations]; 

任何人都可以幫助我說:

  1. 這段代碼有什麼問題?
  2. 什麼可以解決這個問題?

-----------------------------------更新-------- ----------------------

我想那樣的代碼,但它無法正常工作或:

// ------------------------ 
// --- animation ---------- 
// ------------------------ 

- (void)animateIn 
{ 
[UIView beginAnimations: @"animateIn" context:nil]; 
[UIView setAnimationDuration: 0.2]; 

[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

[control setBackgroundColor:[UIColor blackColor]]; 

[UIView commitAnimations]; 
} 

- (void)animateOut 
{ 
[UIView beginAnimations: @"animateOut" context:nil];  
[UIView setAnimationDuration: 0.2]; 

[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

[control setBackgroundColor:[UIColor whiteColor]]; 

[UIView commitAnimations]; 
} 


- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{ 
if([animationID isEqualToString: @"animateIn"]) 
{ 
    [self animateOut]; 
    return; 
} 
else if ([animationID isEqualToString: @"animateOut"]) 
{ 
    cycleCount++; 

    if(cycleCount < 3) 
     [self animateIn]; 
    else 
     cycleCount = 0; 

    return; 
} 
} 

@end 

回答

2
//isFlickerOn is declared in .h file as BOOL isFlickerOn; 
//flickerTimer is declared in .h file as NSTimer *flickerTimer; 
//flickerCount is declared in .h file as int flickerCount; 
//control is a UILabel, UIButton background color is really hard to notice 
//especially the roundedRect UIButton, so I just flickered a UILabel's textColor 

-(void)flickerOn { 
    if (flickerCount < 5) 
    { 
    flickerCount++; 
     if (!isFlickerOn) 
     { 
      [control setTextColor:[UIColor yellowColor]]; 
      isFlickerOn = YES; 
     } 
     else 
     { 
      [control setTextColor:[UIColor blueColor]]; 
      isFlickerOn = NO; 
     } 
    } 
    else 
    { 
     [flickerTimer invalidate]; 
    } 
} 


-(void)flickerAnimation { 
    flickerCount = 0; 
    flickerTimer = [NSTimer scheduledTimerWithTimeInterval:0.3f target:self selector:@selector(flickerOn) userInfo:nil repeats:YES]; 
} 
+1

謝謝JustinXXVII,它似乎在工作。我已經像這樣做了! – wh1t3cat1k 2010-11-11 05:48:03

1

所有 UIView的屬性都是可以動畫的 - 我不相信「高亮顯示」或「選定」。

典型地,它是用於非布爾值可用,像「中心」,「阿爾法」,「幀」和「邊界」。

嘗試調整阿爾法相反,你會看到它的工作。

1

您需要創建的是,當第一個動畫完成執行的回調方法。您使用該回調來創建取消選擇的動畫。請記住,沒有像透明度那樣的逐漸「選擇狀態」。你必須使用

[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
[UIView beginAnimations:@"animateIn" context:NULL]; 

從蘋果的文檔:

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 

您的方法必須採用下列參數:

animationID

一個NSString包含一個可選的應用程序提供的標識符。這是傳遞給beginAnimations:context:方法的標識符。這個說法可能是零。

finished

包含布爾值的NSNumber對象。如果動畫在停止前運行完成,則值爲YES,否則爲NO。

上下文

可選應用程序提供的上下文。這是傳遞給beginAnimations:context:方法的上下文數據。這個說法可能是零。

當動畫結束時,回調animationDidStop被調用並傳入字符串@「animateIn」。您可以使用該方法來檢查哪個動畫被調用並在那裏處理。

+0

我試過了,但它不起作用。看起來像在執行動畫之前調用「animationDidStop」方法。沒有更改可見。請看看更新後的帖子。 – wh1t3cat1k 2010-11-09 20:24:51

+0

該代碼看起來很完美,但[UIColor whiteColor]和+ blackColor可能不是「可生成的」。如果你只是對「閃爍」感興趣,那麼這不是一個真正的「動畫」。你會更適合於改變顏色的NSTimer。我會爲該場景添加一個新答案。在您的動畫塊中,嘗試將animateOut中控件的alpha值更改爲0.0f。在animateIn中將其更改爲1.0f。將持續時間更改爲0.3f,並且可能更明顯。 – Justin 2010-11-10 14:02:32