奇怪的UIButton行为:是正常吗? [英] Strange UIButton behavior: Is that normal?

查看:150
本文介绍了奇怪的UIButton行为:是正常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的UIButton,想要做的事情,当用户触摸它,但然后移动手指在按钮之外,未触摸屏幕。所以这看起来像我需要监听UIControlEventTouchUpOutside事件。

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];

和相应的操作方法:

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

现在猜猜是什么?我触摸按钮,然后将手指拖动到其外部,触摸屏幕,不记录任何消息。事实上,它会记录我UIControlEventTouchUpInside事件已经发生,即使我的手指不是真的在按钮上方。似乎我可以移动手指大约150%的宽度和高度围绕该按钮,而触摸,它会告诉我,当我的手指触摸按钮,当我untouch。但是当我移动它非常远(=足够远),我得到了touchUpOutside日志消息。所以这只是另一个疯狂从苹果,像延迟在totoheshes和像这样的东西?或者我做错了什么?

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?

推荐答案

touchUpInside

如果你真的要覆盖这个行为,在 touchUpInside 处理程序检查触摸的位置,然后直接调用 touchUpOutside 处理程序,如果触摸按钮的 bounds

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.

这篇关于奇怪的UIButton行为:是正常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆