Animated button

Discussion in 'iOS Programming' started by caldwelljason, Aug 1, 2008.

  1. caldwelljason macrumors member

    Jul 9, 2008
    I need a button that changes state in an animated way when the user touches it.

    I have spent quite a bit of time going down the "make a UIImageView-derived control respond to touches" road with no success. I can get it to display correctly and to animate correctly, but not to respond to touches.

    So I changed approaches. I am now using a UIButton-derived control. UIButton inherits UIControl, which UIImageView is not. So UIButton has an addTarget: action:... method. I figured I could use this method to capture the touch event and just add a UIImageView as a subview of the button.

    No such luck. :confused: Now I get the touch events, but the image doesn't display or animate... :(

    Any thoughts?
  2. caldwelljason thread starter macrumors member

    Jul 9, 2008
    Touch events

    So now I can get the UIImageView to appear as the subview of the button (I had to initialize it with a rect relative to the button, not the container, duh :p), but it is eating all the touch events!

    When I used a UIImageView-derived class, I couldn't BEG it respond to touch events, they always flowed to the control behind it.

    Now that I stick a UIImageView directly on a button, it is eating all of the events! Me no like.
  3. caldwelljason thread starter macrumors member

    Jul 9, 2008
    One solution

    OK. So here's the punt of the week:

    I created a UIImageView derived class to handle the drawing and animation, etc. Then, in the initialization of said class, I create a UIButton object and lay it over the UIImageView, like so:

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
    [button addTarget:self action:mad:selector( touchedImage: ) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    [button release];

    So now the selector set up for the UIButton control handles the touch event and the UIImageView handles the drawing and animating.

    I'm not real happy with it. Feels a little duct-tape-ish. But it works like a charm...;)

Share This Page