例如,我有一个放在 IB 中的 UIButton
。
这个 UIButton
有两个功能,具体取决于它是在内部触摸还是在外部拖动 - 至少让它在内部触摸时保持功能会很好。
我想点击我的按钮并“拖到外面”,但是当我最终释放点击时在矩形上使用 -touchesEnded
。
我知道一个选择是放弃 UIButton
并在掩码上使用 -touchesBegan
,但这需要更多的努力并且需要一个很好的大重写(考虑96 个按钮)。
所以稍微改一下 - 我点击了一个按钮并按住鼠标,同时将光标(/手指)拖到我 View 中的不同点,我希望我的 View 识别 -touchesEnded
当我终于放手的时候。问题是初始点击与按钮有关,所以 -touchesEnded
在我的主视图中不会发生...
我是否尝试以错误的方式执行此操作?另外,我说清楚了吗?
编辑:@Heckman 在下面很好地表达了这个问题
最佳答案
如果我正确理解你的问题,你正在触摸一个按钮,它包含在一些 UIView 中,并希望能够将你的手指拖出,并识别按钮 super View 中的点。
在我看来你应该从按钮检测按钮按下,然后调用 [UIButton superview]
,告诉 superview 关于开始的按钮按下,然后处理在 View 中触摸结束。
要通知 super View 按下的按钮调用类似(从您的 UIButton):
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.superview buttonPressed:self]
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self.superview touchesEnded:touches withEvent:event];
}
在您的 super View 代码中(如果您有一个属性 buttonPressed):
-(void) buttonPressed:(UIButton *) button {
_buttonPressed = button;
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(self.buttonPressed) {
//Process the release from your drag here.
for(UITouch * touch in touches) {
//Use either of these to get the release position that you care about.
CGPoint releaseLocationInCurrentView = [touch locationInView:self];
CGPoint releaseLocationInButtonView = [touch locationInView:[self.buttonPressed]];
}
}
}
关于iphone - 开始点击 UIButton,但在主视图中使用 touchesEnded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10592559/