【发布时间】: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...