Detecting when a custom UIButton has been pressed

I created a custom UIButton (subclass of UIButton) for my application. Under this button, how would I change the background color when clicked? (It should return to its normal background when depressed. I am currently using Core Graphics to paint a gradient background.)

I tried forking in drawRect: (checking for self.selected and self.highlighted , but no changes happen because drawRect not called in the touch event.

Any suggestions or code examples are welcome.

+8
ios iphone cocoa-touch uibutton subclass
source share
1 answer

Try calling [self setNeedsRedraw] on the touch event.


Asker provided a solution based on this answer and comments below.

Decision

B:

 [self addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchDown]; [self addTarget:self action:@selector(buttonDepressed) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside | UIControlEventTouchCancel]; 

If selectors are user-defined functions that perform a background change.

+15
source share

All Articles