0

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

所以基本上当他们将手指按在按钮上时,图像会发生变化,当他们松开时,我在 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;
}
4

1 回答 1

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;

inviewDidLoad而不是 in IBAction,然后只需确保您的按钮 inInterface Builder正确链接到IBOutlet您为其创建的按钮。

于 2012-04-04T01:49:19.803 回答