1

如何在一段时间内更改 UIButton 颜色,如 5-6 秒?

4

5 回答 5

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] ;
    }
    

这是找到的好方法。

于 2012-04-11T08:03:09.107 回答
3

像这样的东西:

  [button setBackgroundColor:[UIColor coloryouneed]];
  [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]];
  [button setBackgroundColor:[UIColor previousColor]];
于 2012-04-11T07:50:12.807 回答
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 方法改变颜色

于 2012-04-11T07:51:40.137 回答
1

你 NSTimer。为按钮设置颜色。[button setBackgroundColor:[UIColor redColor]];. 安排一个 NSTimer 来重置它。

[NSTimer timerWithTimeInterval:5 target:self selector:@selector(resetColor) userInfo:nil repeats:NO];
于 2012-04-11T07:47:16.110 回答
1

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

于 2012-04-11T07:48:51.317 回答