2012-07-17 189 views

回答

3

你會發現這是很有幫助的,這個片段在挑選每random 3.5秒的顏色和動畫化從什麼都它是目前到新的backgroundColor

- (void)viewDidLoad 
{  
[super viewDidLoad]; 

    NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval: 3.5 
           target: self 
           selector: @selector (updateBackgroundColor) 
           userInfo: nil 
           repeats: YES]]; 

} 
- (void) updateBackgroundColor { 

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

    CGFloat redLevel = rand()/(float) RAND_MAX; 
    CGFloat greenLevel = rand()/(float) RAND_MAX; 
    CGFloat blueLevel = rand()/(float) RAND_MAX; 

    myButton.backgroundColor = [UIColor colorWithRed: redLevel 
               green: greenLevel 
               blue: blueLevel 
               alpha: 1.0]; 
    [UIView commitAnimations]; 
} 
+0

[自setBackgroundColorTimer:的NSTimer scheduledTimerWithTimeInterval:1.0 --->這行代碼被撞壞.....錯誤顯示setBackgroundColorTimer:]:無法識別的選擇發送到實例0x4b159b0' 那樣 – iCrazyDev 2012-07-17 05:56:56

+0

@rajeshkumar抱歉在我的答案中嘗試編輯的代碼。 – 2012-07-17 06:00:17

+0

@rajeshkumar請記住,修改定時器間隔意味着你將不得不調整「[UIView setAnimationDuration:3.0]; 」來匹配它。 – 2012-07-17 06:05:25