【问题标题】:long press and touchesBegan长按和触摸开始
【发布时间】:2014-05-04 08:49:14
【问题描述】:
当我在 ViewController 中检测到长按时,我会显示弹出菜单(添加 UIView 作为子视图)。当长按结束时,我会隐藏我的菜单(从超级视图中删除 UIView)。所以我的菜单只有在用户触摸屏幕时才可见。问题是当我按住并移动手指而不触摸时,我的菜单不会调用touchesBegan 或touchesMoved,所以我无法从菜单中选择任何按钮。除了从ViewController 传递事件之外,还有其他方法吗?我想在我的UIView 中执行此操作。请帮忙。
【问题讨论】:
标签:
ios
uiview
touchesbegan
touchesmoved
【解决方案1】:
您最好的选择是将UIPanGestureRecognizer 添加到您的 ViewController 的视图中。
像这样:
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[self.view addGestureRecognizer:panGestureRecognizer];
在实现handlePanGesture 中,您应该找到识别器相对于您的弹出视图的翻译。
-(void)handlePanGesture:(id)sender {
UIPanGestureRecognizer *recognizer = sender;
if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:self.contentView];
//Here you can use translation to detect what button touched with gesture
}
}