14

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];
            }
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Sam007
  • 1,385
  • 3
  • 17
  • 32

6 Answers6

14

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.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
taskinoor
  • 45,586
  • 12
  • 116
  • 142
6

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;
}
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
visakh7
  • 26,380
  • 8
  • 55
  • 69
2

In Swift:

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

jxmorris12
  • 1,262
  • 4
  • 15
  • 27
1

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];
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
0

@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];
    }
}
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
JScarry
  • 1,507
  • 1
  • 13
  • 25
-1

to change the image immediately use the backgroundImage property.

Magic Bullet Dave
  • 9,006
  • 10
  • 51
  • 81