IPhone UIButton - Swipe vs. Touches
I subclassed a UIButton for custom drawing (overriding drawRect
) and some other custom behavior, out of which the following is relevant (and trivial):
- If it is pressed (
UIControlEventTouchDown
) a flag "pressed" is turned on (affects custom drawing, kind of like "highlighted"). - If it is released inside (
UIControlEventTouchUpInside
), myOnClick
function is triggered. - If it is released outside (
UIControlEventTouchUpOutside
), flag "pressed" is turned off.
I now wanted to add an additional behavior to the button so it will recognize swipe gestures (using UISwipeGestureRecognizer
), and affect the button content. The problem is that when preforming a swipe gesture, the TouchDown
fires, as it really should, causing the button to appear "pressed", which is not my intention during a swipe.
My question is if there is an elegant way to avoid this. Perhaps something similar to DelaysContentTouches
of a scroll view, that will separate the swipe behavior from the pressing behavior. The only other alternative I can see, is messing around with timers my self开发者_高级运维, which I would prefer avoiding if possible.
Thanks!
DannyA. Here is my solution:
Use also UITapGestureRecognizer
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapAction:)];
Then, implement this in didTapAction:
selector
[UIButton setHighlighted:YES];
[UIButton performSelector:@selector(setHighlighted:) withObject:NO afterDelay:0.15];
That work fine for me.
精彩评论