【问题标题】:UIButton UIControlEventTouchUpInside triggering event even when picking up finger outside of button frameUIButton UIControlEventTouchUpInside 触发事件,即使在按钮框架之外拿起手指
【发布时间】:2014-03-31 09:31:41
【问题描述】:

我需要在我的 iPhone 应用程序上有一个按钮,该按钮仅在用户在框架区域内触摸时触发事件。

据我所知,UIControlEventTouchUpInside 应该这样做,并且在大多数情况下是这样做的:如果我将手指在屏幕上拖离框架足够远,然后才抬起它,则不会触发事件(这是我正在寻找的行为。)

但是,如果我在框架外抬起手指,无论如何都会触发事件。有没有办法防止它?

UIButton *selectPlan = [UIButton buttonWithType:UIButtonTypeRoundedRect];
selectPlan.frame = CGRectMake(self.view.bounds.size.width/2-buttonWidthHeight/2, self.view.bounds.size.height/2-buttonWidthHeight/2 + 43, buttonWidthHeight, buttonWidthHeight);
[selectPlan addTarget:self action:@selector(checkIfPlansExistAndFindBestPlan) forControlEvents:UIControlEventTouchUpInside];
[selectPlan setTitle:@"Select Plan" forState:UIControlStateNormal];
[self.view addSubview:selectPlan];

【问题讨论】:

  • 我认为这不应该发生...您可以通过给 UIButton 提供边框来检查按钮的框架... (stackoverflow.com/a/8162518/1066828)
  • 我在 iOS 6 和 7.1 中测试了您的代码,并且运行良好。在模拟器中测试并用鼠标箭头触摸。
  • @FahimParkar 我做到了。它确实发生了。
  • @Gabriel.Massana,你确定吗?尝试将光标拖到按钮边框之外,不要拖得太远。
  • @Eddy :这似乎是 UIButton 的默认性质。如果您想要确切的位置,请使用 touchEvents...

标签: ios ios7 uibutton


【解决方案1】:

这是 UIButton 的默认特性。你不能改变它。

从我理解的按钮名称来看,你有计划,使用时选择计划你想找到哪个按钮被点击。

为了更准确,我想说使用 touchesBegantouchesEnd 事件。

以下是这些活动的示例链接。

http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_Touch,_Multitouch_and_Tap_Application

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 2013-11-04
    • 2019-02-26
    相关资源
    最近更新 更多