Highlighting a UIControl subclass

user370773 picture user370773 · Dec 13, 2010 · Viewed 8.7k times · Source


basically I need an image button, specifically a custom object that:
1) calls a controller's action when tapped
2) encapsulates custom data
3) is automatically moved by wrapper view (not relevant)

Well, I got all this with a subclass of UIControl (since subclassing UIButton is not recommended and subclassing UIImageView makes difficult to manage the point 1). But now what is the correct way to highlight it? I would like to highlight the control when tapped in any way (even a simple momentary reduction of alpha).

With beginTrackingWithTouch and endTrackingWithTouch I can't recognize the only UIControlEventTouchUpInside event.

A view animation in the controller? It seems to me a rough solution

Is there a simple and immediate solution?

Thanks :(

Answer

Costique picture Costique · Dec 13, 2010

I think UIControls automatically set their highlighted property correctly, based solely on touch events. What you need is to override -setHighlighted: method to implement a specific algorithm:

- (void) setHighlighted: (BOOL) highlighted {
    [super setHighlighted: highlighted];
    // Only as an example. Caution: looks like a disabled control
    self.alpha = highlighted ? 0.5f : 1.0f;
}