0

我有 4 个UIButtons我想以随机顺序闪烁较浅的彩色图像一秒钟。所以很明显我想以编程方式更改按钮图像。正如您在XCode Project方法中看到的:showSequence,它尝试将图像设置为较亮的图像,然后再返回,但它不会显示在模拟器上。

代码片段:

- (void) showSequence:(NSMutableArray *)sequenceArray {

    for(int i = 0; i < [sequenceArray count]; i++){

        int sequenceNumber = [[sequenceArray objectAtIndex:i] intValue];

        switch (sequenceNumber) {
           case 1: {
              NSLog(@"green");
              [self.greenButton setBackgroundImage:[UIImage imageNamed:@"green_active.png"] forState:UIControlStateNormal];
              sleep(1);
              [self.greenButton setBackgroundImage:[UIImage imageNamed:@"green_inactive.png"] forState:UIControlStateNormal];
              break;
           }

           case 2: {
              NSLog(@"red");
              [self.redButton setBackgroundImage:[UIImage imageNamed:@"red_active.png"] forState:UIControlStateNormal];
              sleep(1);
              [self.redButton setBackgroundImage:[UIImage imageNamed:@"red_inactive.png"] forState:UIControlStateNormal];
              break;
           }

          case 3: {
             NSLog(@"yellow");
             [self.yellowButton setBackgroundImage:[UIImage imageNamed:@"yellow_active.png"] forState:UIControlStateNormal];
             sleep(1);
             [self.yellowButton setBackgroundImage:[UIImage imageNamed:@"yellow_inactive.png"] forState:UIControlStateNormal];
             break;
          }

          case 4: {
             NSLog(@"blue");
             [self.blueButton setBackgroundImage:[UIImage imageNamed:@"blue_active.png"] forState:UIControlStateNormal];
             sleep(1);
             [self.blueButton setBackgroundImage:[UIImage imageNamed:@"blue_inactive.png"] forState:UIControlStateNormal];
             break;
        }

        default:
            break;
       }
    }
}

在此先感谢(顺便说一句,在它会中断的序列中不要点击开始按钮(我仍在努力))

4

2 回答 2

1

您可以通过简单地将 imageView 添加到 Button 并将 NSArray 分配给 imageView.animationImages 属性来做到这一点。NSArray 必须包含按照您的序列的图像,请参见下面的代码

 imageView.animationImages = [NSArray arrayWithObjects:    
                                        [UIImage imageNamed:@"myImageFrame1.gif"],
                                        [UIImage imageNamed:@"myImageFrame2.gif"],
                                        [UIImage imageNamed:@"myImageFrame3.gif"],
                                        [UIImage imageNamed:@"myImageFrame4.gif"], nil];


        imageView.animationDuration = 2.0; 
        imageView.animationRepeatCount = 1; // for showing only once, otherwise set to 0
        [imageView startAnimating]; 
[self.button addsubView:imageView]
于 2013-06-11T04:41:03.977 回答
0

查看

  1. 正确添加和命名的图像
  2. 查看按钮上是否添加了图像,则背景图像不可见。
  3. IBoutlets 已连接
  4. 而不是setBackgroundImage:尝试setImage:
于 2013-06-11T03:49:40.590 回答