UIButton - On touch change image

2019-02-08 08:30发布

When I touch the button at that time I want to change image & when i release the touch button image is as it is.

I want to apply below code but it's not with my expectation.

please give me any suggestion.....

   -(IBAction)actionEnter:(id)sender{
            if ([sender isSelected]) {
                [sender setImage:[UIImage imageNamed:@"enter-hover.png"] 
                        forState:UIControlStateNormal];
                [sender setSelected:NO];
            } else {
                [sender setImage:[UIImage imageNamed:@"enter.png"] 
                        forState:UIControlStateSelected];
                [sender setSelected:YES];
            }

6条回答
Juvenile、少年°
2楼-- · 2019-02-08 09:09

to change the image immediately use the backgroundImage property.

查看更多
The star\"
3楼-- · 2019-02-08 09:10

In Swift:

button.setImage(UIImage(named: "enter.png"), forState: [.Selected, .Highlighted])

查看更多
地球回转人心会变
4楼-- · 2019-02-08 09:13

You can use UIControlStateHighlighted for this.

[myButton setImage:[UIImage imageNamed:@"enter-hover.png"] 
          forState:UIControlStateHighlighted];

You can also set this from interface builder by setting the image for highlighted state.

查看更多
放荡不羁爱自由
5楼-- · 2019-02-08 09:17

I think, you could set the image in the beginning for normal and selected state ..

Try with below when you create the UIButton object. [Use the images as per your requirement]

[myButton setImage:[UIImage imageNamed:@"enter.png"] 
          forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:@"enter-hover.png"] 
          forState:UIControlStateSelected];
查看更多
Luminary・发光体
6楼-- · 2019-02-08 09:19

I think this should do it. Set the images after creating the button

[yourButton setImage:[UIImage imageNamed:@"enter-hover.png"] 
            forState:UIControlStateSelected];
[yourButton setImage:[UIImage imageNamed:@"enter.png"]  
            forState:UIControlStateNormal];

and do this

- (IBAction)actionEnter:(id)sender{
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;
}
查看更多
迷人小祖宗
7楼-- · 2019-02-08 09:34

@7KV7 got me thinking. I have favorite and ignore buttons that I want to use to mark favorite pictures and pictures that I never want to see again. I used his method to initialize the buttons and then slightly modified his method to toggle the buttons on and off.

In this example, if you mark a picture as a favorite, you want to turn off the ignore button and vice versa. The delegate handles the database stuff.

 self.favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.ignoreButton   = [UIButton buttonWithType:UIButtonTypeCustom];

        [self.favoriteButton setImage:[UIImage imageNamed:@"Favorite-Selected"] 
                             forState:UIControlStateSelected];
        [self.favoriteButton setImage:[UIImage imageNamed:@"Favorite"] 
                             forState:UIControlStateNormal];

        [self.ignoreButton setImage:[UIImage imageNamed:@"Ignore-Selected"] 
                           forState:UIControlStateSelected];
        [self.ignoreButton setImage:[UIImage imageNamed:@"Ignore"] 
                           forState:UIControlStateNormal];

If you are just toggling a button on or off, you won’t need to make it a property, since the buttonPressed sender knows which button has been pressed. I need to have them be property since I need to tell the opposite button to turn its highlight off.

- (void)favoriteIgnore:(UIButton *)buttonPressed {

     // Toggle the tapped button
     buttonPressed.selected = ( buttonPressed.selected) ?  NO : YES;

    id <ScoringToolbarDelegate> TB_delegate = _delegate;

    // Turn off the other button and call the delegate
    if ([buttonPressed.currentTitle isEqualToString:@"favorite"]) {

        self.ignoreButton.selected = NO;
        [TB_delegate favoriteButtonPressed];

    } else {

        self.favoriteButton.selected = NO;
        [TB_delegate ignoreButtonPressed];
    }
}
查看更多
登录 后发表回答