UIButton - On touch change image

Sam007 picture Sam007 · Apr 18, 2011 · Viewed 24.2k times · Source

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];
            }

Answer

taskinoor picture taskinoor · Apr 18, 2011

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.