views:

417

answers:

2

I have a simple UIButton and want to do something when the user touches it, but then moves the finger outside the button and untouches the screen. So that seems like I need to listen for the UIControlEventTouchUpOutside event.

In my view controller, I did this:

UIButton *bt = [[UIButton alloc] initWithFrame:rect];
[bt setBackgroundColor:[UIColor whiteColor]];
[bt addTarget:self action:@selector(onTouchUpOutside) forControlEvents:UIControlEventTouchUpOutside];
[self.view addSubview:bt];

and the corresponding action method:

- (void)onTouchUpOutside {
    NSLog(@"touchUpOutside");
}

now guess what? I touch the button, then drag the finger outside of it, untouch the screen and no message is logged. Indeed it would log me that an UIControlEventTouchUpInside event has happened, even if my finger is not really above the button. It seems like I can move the finger about 150% of the width and hight around that button while touching, and it will tell me that the finger was in the button when I untouch. But when I move it very far away (= far away enough), I do get that "touchUpOutside" log message. So is that just another madness from apple, like the delay in -touchesMoved and stuff like that? Or did I do something wrong?

A: 

I figured out that this happens to be normal UIKit behaviour. Strange, but normal. The user must move the finger far away enough in order to "cancel" a touch. If the finger is not far away enough from the touched button, and liftet, then a touchUpInside event is fired. Couldn't find text from apple saying this, but I tested several times in several projects, including a brand new one with a plain UIButton an an IBAction. Also some sample code from apple shows this same strange behavior.

HelloMoon
+2  A: 

touchUpInside is fired when you move your finger a bit outside the button, because people have big, imprecise fingers.

If you really want to override this behaviour, in a touchUpInside handler check the location of the touch, then call the touchUpOutside handler directly if the touch it outside the button's bounds.

iKenndac
ok so that's because of usability...I just wonder why that area is so incredible huge ;-)
HelloMoon
People have fatter fingers than you think, also they are dumber.
Corey Floyd