简体   繁体   中英

Buttons changing background images on iPhone

I have it working with a small error. I have two code pieces for each button. One being this:

-(IBAction)clickoneButton:(id)sender
{
    [sender setBackgroundImage:[UIImage imageNamed:@"cnumber_1.jpg"] forState:UIControlStateNormal];
}

And the other being this:

-(IBAction)oneButton:(id)sender
{
    self.enteredPhoneNumberString = [self.enteredPhoneNumberString stringByAppendingString:[NSString stringWithFormat:@"%d", 1]];
    [self updateFormattedPhoneNumberLabel];

    [sender setBackgroundImage:[UIImage imageNamed:@"Number_1.jpg"] forState:UIControlStateNormal];
}

The first is set up to be Touch Down. The other is for Touch Up Inside. The app looks like the phones regular dialer. So the first image is a light blue button and the second is a dark blue button just the same way as the iPhones dialer is. The issue is when I press down on the button it almost seems like the dark image is still behind the Touch Down light blue image. It is changing to a lighter colored image but is shaded almost like the dark image is transparent through it or something. Not sure how else to explain it. If you have any ideas as to why the light blue is darkened some when the Touch Down is happening I would appreciate any help. Thanks

触摸前和释放后的按钮 Button before touch and after release (Correct)

按下按钮 Button on Touch Down (not working properly)

“ Touch Down”按钮应该是什么样的 What the touch down button should look like

Based upon the two snippets of code you provided, you are not setting an image for when the button is pressed. Try updating your code to use the following when you load the view:

// sets the background image for when the button is not pressed
[sender setBackgroundImage:[UIImage imageNamed:@"cnumber_1.jpg"] forState:UIControlStateNormal];

// sets the background image for when the button is pressed
[sender setBackgroundImage:[UIImage imageNamed:@"Number_1.jpg"] forState:UIControlStateHighlighted];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM