简体   繁体   English

在另一个视图控制器中检测到LongPressGesture已结束

[英]Detecting a LongPressGesture has ended in another view controller

I have a LongPressGesture recogniser which when a long press is detected it presents a new segue: 我有一个LongPressGesture识别器,当检测到长按时它会显示一个新的序列:

if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {

            [self performSegueWithIdentifier:@"showImage" sender:self];
}

The problem being i want the new 'segue' to detect that the gesture had ended and revert back to the previous view controller : 问题是我希望新的“ segue”检测到手势已结束并返回到先前的视图控制器:

if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {

   [self presentViewController:friendViewController animated:YES completion:Nil];
}

I have tried to set up a new gesture recogniser in the new segue but it isn't detected unless the user ends the previous gesture. 我试图在新的序列中设置一个新的手势识别器,但是除非用户结束以前的手势,否则不会检测到它。

You're doing (or trying to do) several things incorrectly. 您正在错误地执行(或尝试执行)几项操作。 When you segue to the new controller, the long press recognizer's state will go to "failed", so there's nothing more you can do with it. 当您选择新控制器时,长按识别器的状态将变为“失败”,因此您无能为力了。 There's no way to add a gesture recognizer to the new controller's view that will accept your previous touch as the beginning of its touch, so that's not going to work. 无法在新控制器的视图中添加将您以前的触摸作为其触摸开始的手势识别器,因此将无法正常工作。 Also, if you want to go back to your previous controller, you shouldn't use presentViewController, that just creates a new instance of friendViewController; 另外,如果您想回到以前的控制器,则不应该使用presentViewController,它只是创建一个新的friendViewController实例; it doesn't go back to the old one. 它不会回到旧的。

I think the way you need to accomplish your goal, is not to present a new controller, but to add a new view on top of the one with the gesture recognizer. 我认为,实现目标所需的方式不是展示新的控制器,而是在带有手势识别器的视图之上添加新视图。 In the example below, I just create a simple view for demonstration purposes, but you could create one in a xib if you need something more complicated. 在下面的示例中,我只是出于演示目的创建一个简单的视图,但是如果需要更复杂的功能,则可以在xib中创建一个视图。

-(IBAction)handleLongPress:(UILongPressGestureRecognizer *)sender {

    if (sender.state == UIGestureRecognizerStateBegan) {
        UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds];
        newView.backgroundColor = [UIColor redColor];
        newView.tag = 10;
        [self.view addSubview:newView];
    }

    if (sender.state == UIGestureRecognizerStateEnded) {
        [[self.view viewWithTag:10] removeFromSuperview];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检测以前的视图控制器 - Detecting the previous view controller 单元测试,测试一个视图 controller 是否已经呈现另一个视图 controller - Unit test, to test if a view controller has presented another view controller 检测标签栏选中的视图控制器 - Detecting tabbars selected view controller 如何添加一个已经有父 controller 的视图 controller 作为另一个视图 controller 的子视图 - How do I add a view controller that already has a parent controller as a child of another view controller 获取添加了longpressGesture识别器的视图坐标 - Get Coordinates of view on which longpressGesture recogniser is added LongpressGesture端不会关闭视图 - LongpressGesture end won't dismiss view 如何从具有另一个视图的视图中获取视图 controller 具有视图 - How do get a view from a view that has another view controller with a view 在以模态呈现另一个视图控制器后,视图控制器会发生什么 - What happens to a View Controller after it has modally presented another View Controller 检测到按下后背可转到当前视图控制器 - Detecting that back was pressed to get to current view controller 检测用户结束的触摸屏 - Detecting user ended touching screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM