2012-04-11 62 views

回答

3

是这样的:

[button setBackgroundColor:[UIColor coloryouneed]]; 
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]]; 
    [button setBackgroundColor:[UIColor previousColor]]; 
1

你NSTimer。将颜色设置为按钮。 。 安排NSTimer重置它。

[NSTimer timerWithTimeInterval:5 target:self selector:@selector(resetColor) userInfo:nil repeats:NO]; 
1

创建一个改变颜色的线程。通过使用sleep(randomValue);你可以使你的间隔。

2

在.h文件中创建的NSTimer的财产比.m文件

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 

{ 

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

    if (self) { 

self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(changebtncolor) userInfo:nil repeats:YES]; 

    } 

    return self; 
} 

现在ü可以在changebtncolor方法改变颜色,下面写代码

6

按照这些步骤操作:

  1. 将2个图像添加到资源文件夹(比如红色和蓝色)。

  2. 在XIB上拖动一个按钮,将其属性更改为自定义按钮,并将其背景图像(设置为blue.png)。

  3. viewDidLoad方法做到这一点:

    [NSTimer scheduledTimerWithTimeInterval:0.8 
               target:self 
               selector:@selector(changeColor:) 
               userInfo:nil 
               repeats:NO]; 
    
  4. 实现此方法:

    -(void)changeColor:(id)sender 
    { 
        [btnTemp setBackgroundImage:[UIImage imageNamed:@"red.png"] 
             forState:UIControlStateNormal] ; 
    } 
    

这是找到了好办法。

+0

我第一次尝试,它工作正常,但当我第二次没有工作.... – iosDev 2012-04-11 09:00:09

+0

一个小警告:如果你需要重复的效果,移动'NSTimer'设置为适当的方法。否则,这是做**的正确方法。 – Costique 2012-04-12 05:11:56

相关问题