ios - 如何在屏幕上已经有触摸的情况下激活 UIGestureRecognizer?

标签 ios objective-c uikit uigesturerecognizer uitouch

我已经使用 UIPanGestureRecognizer 实现了一些交互式 UIViewController 演示动画,例如平移以呈现 View Controller 。

我现在要做的是允许这种行为通过 View Controller 动画之一继续。因此,如果我慢慢向上平移,它会呈现一个 View Controller ,我希望属于新呈现的 View Controller 的手势识别器无缝拾取属于先前呈现的 View 的 UITouch。

这个想法是,如果你从 VC1 开始,然后慢慢地平移,它会呈现一个 View Controller 。如果继续平移,它将呈现另一个 View Controller 。

这最好由属于堆栈中每个 ViewController 的 UIPanGestureRecognizer 完成。

有没有人做过这样的事情?我目前正在尝试在显示 VC 后取消上一个手势识别器,但我没有看到下一个 VC 的手势识别器变为事件状态...

预先感谢您提供的任何见解!

最佳答案

手势识别器属于 View 而不属于 View Controller 。应用程序中的顶级 View 是 UIWindow 实例。您可以将平移手势识别器添加到窗口。

在 Storyboard中,我将 UINavigationController 设置为初始 View Controller 。

Storyboard

我为测试我的想法而编写的唯一代码是这个

#import "AppDelegate.h"

@interface AppDelegate () {
    CGFloat pos;
    int cnt;

}

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self.window addGestureRecognizer:panRecognizer];

    return YES;
}

-(void) handlePan:(UIPanGestureRecognizer*)recognizer {

    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan: {
            pos = [recognizer locationInView:self.window].y;
            break;
        }
        case UIGestureRecognizerStateChanged: {
            CGFloat newPos = [recognizer locationInView:self.window].y;
            if (pos - newPos > 50) {
                UIViewController* nextVC = [[UIViewController alloc] init];

                nextVC.view.backgroundColor = (cnt++)%2 ? [UIColor redColor] : [UIColor blueColor];
                UINavigationController* navCtrl = (UINavigationController*)self.window.rootViewController;
                [navCtrl pushViewController:nextVC animated:YES];
                pos = newPos;
            }
            break;
        }
        default:
            break;
    }
}

@end

关于ios - 如何在屏幕上已经有触摸的情况下激活 UIGestureRecognizer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37493065/

相关文章:

ios - 在为另一个节点使用灯光后保持原始节点照明

iPhone : How to remove right button from navigation bar in a view controller?

ios - 协议(protocol)不适用于容器

ios - 是否可以在 iOS 6/7 上自定义数字键盘中的左下角按钮?

ios - 无法让 UILabel 的文本停留在多行

objective-c - ASIHttp 框架的 Apple Mach-O 链接器 (Id) 错误

iphone - 静音内置 iPhone 麦克风

objective-c - SQLite 数据库图像在 Xcode 中构建时出现格式错误

ios - UIGraphicsGetImageFromCurrentImageContext 内存泄漏与预览

cocoa-touch - 制作多行 UILabel 从顶部而不是中间开始文本