长按和触摸开始

时间:2014-05-04 08:49:15

标签: ios uiview touchesbegan touchesmoved

当我在ViewController我显示弹出菜单中检测到长按时(将UIView添加为子视图)。当长按结束时,我隐藏了我的菜单(从superview中删除UIView)。所以我的菜单只有在用户触摸屏幕时才可见。问题是当我握住并移动手指而没有触摸时,我的菜单不会调用touchesBegantouchesMoved,因此我无法从菜单中选择任何按钮。除了从ViewController传递事件之外还有其他方法吗?我想在我的UIView中这样做。求助。

1 个答案:

答案 0 :(得分:1)

您最好的选择是将UIPanGestureRecognizer添加到ViewControllers的视图中。 像那样:

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
    }
}
相关问题