UIImage按钮(圆角矩形按钮)

时间:2012-04-04 01:38:20

标签: objective-c ios

我希望能够在用户按下按钮时更改任何按钮的图像,然后当它们释放时,之前存在的图像被放回。

所以基本上当他们用手指按下按钮时,图像会改变,当它们释放时,我在IB中指定的原始图像被放回去。

我该怎么做?这是我到目前为止的代码。

- (IBAction)btnOne:(id)sender {
    result.text = [self appendresult:@"1"];

    //Changes the button image upon touch
    [btnOne setImage:[UIImage imageNamed:@"NumberBtn1.png"] forState:UIControlStateNormal]; 
    [btnOne setImage:[UIImage imageNamed:@"NumberBtn1Alt.png"] forState:UIControlStateHighlighted];
    [btnOne setImage:[UIImage imageNamed:@"NumberBtn1Alt.png"] forState:UIControlStateSelected];
    btnOne.showsTouchWhenHighlighted = YES;
}

1 个答案:

答案 0 :(得分:1)

把你的代码......

  [btnOne setImage:[UIImage imageNamed:@"NumberBtn1.png"] forState:UIControlStateNormal]; 
    [btnOne setImage:[UIImage imageNamed:@"NumberBtn1Alt.png"] forState:UIControlStateHighlighted];
    [btnOne setImage:[UIImage imageNamed:@"NumberBtn1Alt.png"] forState:UIControlStateSelected];
    btnOne.showsTouchWhenHighlighted = YES;

viewDidLoad而不是IBAction中,只需确保Interface Builder中的按钮与您为其创建的IBOutlet正确关联。