【问题标题】:Adjusting position together with interactivePopGestureRecognizer与 interactivePopGestureRecognizer 一起调整位置
【发布时间】:2013-09-26 09:30:19
【问题描述】:

我有一个自定义控件,其中包含一排按钮,模仿标签栏。当UINavigationController 导航离开根视图控制器时,此控件滑出视图,并在导航到根时滑回。

在 iOS 7 中,UIScreenEdgePanGestureRecognizer 提供滑动返回手势。因此,我正在修改我的自定义控件,以便滑动量对应于UIScreenEdgePanGestureRecognizer 的翻译。

问题是,当用户释放触摸时,我如何判断UINavigationController 是导航回还是弹回原始视图?

[self.interactivePopGestureRecognizer addTarget:self action:@selector(panningBack:)];


- (void) panningBack:(UIPanGestureRecognizer *)recognizer
{
    // Snipped - Code that reads the recognizer translation and adjust custom control y position

    if (recognizer.state == UIGestureRecognizerStateEnded)
    {
        // Question: Does it go back, or does it not?

        // If it goes back, slide custom control into view
        // Else slide custom control out of view
    }
}

【问题讨论】:

    标签: iphone ios objective-c uinavigationcontroller ios7


    【解决方案1】:

    我知道这是一个相当古老的问题,所以答案可能对 OP 没有用,但可能对其他人有用。昨天我遇到了同样的问题,并且在网络的其余部分上进行了大量搜索,但没有真正找到任何东西。 所以这是我用于类似问题的解决方案。这是在 navigationcontroller 委托中实现的,但我想如果更适合您的需要,您可以在其他地方进行。

        - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
        id<UIViewControllerTransitionCoordinator> tc = navigationController.topViewController.transitionCoordinator;
        [tc notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) {
            NSLog(@"DONE!!!");
            NSLog(@"Container View: %@", [context containerView]);
            NSLog(@"From VC: %@", [context viewControllerForKey:UITransitionContextFromViewControllerKey]);
            NSLog(@"To VC: %@", [context viewControllerForKey:UITransitionContextToViewControllerKey]);
            NSLog(@"Initially Interactive: %i", [context initiallyInteractive]);
            NSLog(@"Completion Curve: %d", [context completionCurve]);
            NSLog(@"Is Animated: %i", [context isAnimated]);
            NSLog(@"Is Cancelled: %i", [context isCancelled]);
            NSLog(@"Is Interactive: %i", [context isInteractive]);
            NSLog(@"Percent Complete: %f", [context percentComplete]);
            NSLog(@"Presentation Style: %d", [context presentationStyle]);
            NSLog(@"Transition Duration: %f", [context transitionDuration]);
        }];
    }
    

    当用户抬起手指并且动画被反转或完成时,这将触发。 [context isCancelled]; 会告诉你它是反转还是完成。在上下文对象中还有很多其他有用的信息可以使用。

    【讨论】:

    • 非常有用。我很沮丧,Apple 没有提供任何关于如何检查弹出手势是否被取消的文档(或者他们只是假设我们会知道过渡协调器)。太棒了。
    【解决方案2】:

    我想说最简单的解决方案是使用导航控制器中实现的默认手势。当视图出现时显示栏,当它消失时隐藏栏。

    知道是否应该返回的一个优雅的解决方案是检测最后一个动作。

    意味着如果用户向左移动了一些像素并释放 -> 反弹 如果用户向右移动一些像素并释放 -> 显示上一个控制器

    如果您将位置保存为状态更改并将其与状态结束进行比较,则可以做到这一点。

    这样理解重点:

    CGPoint 点 = [识别器 locationInView:view];

    【讨论】:

    • 当用户松开手指时,我如何知道导航是否会返回?我的想法是使用导航动画为我的自定义标签栏设置动画。
    • 我最近为 Pan Gesture 识别器找到了一个更简单的解决方案:您可以使用 velocity 属性:[recognizer velocityInView:self.view]。如果 x 值为正 = 向右移动 = 后退
    猜你喜欢
    • 1970-01-01
    • 2018-10-16
    • 2017-04-06
    • 2018-10-07
    • 1970-01-01
    • 2021-12-12
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多