2012-07-17 188 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