1

I have subclassed UIButton in order to do some custom drawing with CoreGraphics in drawRect:. In order to respond to touch events, I am adding these target/actions:

    [self addTarget:self action:@selector(selectedButton:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragInside];
    [self addTarget:self action:@selector(deSelectedButton:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchDragOutside | UIControlEventTouchUpOutside];

And an example implementation is this:

- (void)selectedButton:(id)sender {
      self.isSelected = YES;
      [self setNeedsDisplay];
}

This works pretty well. The only problem is that if I touch down/up really fast, the action is still processed, but the selected/deSelected methods get called so quickly that the background is never updated. But it works well if I press down for a little longer.

What am I missing here? How can I make my background update for the slightest of touches? I have tried delaying the deSelect call to update the layout, but it has no effect.

Lizza
  • 2,769
  • 5
  • 39
  • 72

1 Answers1

0

i think you probably need to call setNeedsDisplay on the main thread:

[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];

(i don't think whether waitUntilDone is yes or no will make any difference?...)

usrgnxc
  • 794
  • 2
  • 9
  • 34
  • 1
    in that case i'd try some the hacks and workarounds in these answers http://stackoverflow.com/questions/4739748/is-there-a-way-to-make-drawrect-work-right-now (runloop, catransaction etc.) Or alternatively find a way of doing with just switching images instead of custom drawing - i.e. just setImage:forState: OR even just changing between states using predefined images – usrgnxc May 23 '13 at 10:29