【问题标题】:How to activate a UIGestureRecognizer with touches already on screen?如何在屏幕上已触摸的情况下激活 UIGestureRecognizer?
【发布时间】:2016-05-27 22:20:20
【问题描述】:

我已经使用 UIPanGestureRecognizer 实现了一些交互式 UIViewController 演示动画,例如平移以呈现视图控制器。

我现在要做的是允许这种行为通过其中一个视图控制器动画继续。因此,如果我慢慢向上平移,呈现一个视图控制器,我希望属于新呈现的视图控制器的手势识别器能够无缝地拾取属于先前呈现的视图的 UITouch。

这个想法是,如果您从 VC1 开始,然后慢慢向上平移,它将呈现一个视图控制器。如果您继续向上平移,它将呈现另一个视图控制器。

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

有人做过这样的事吗?我目前正在尝试在呈现 VC 后取消上一个手势识别器,但我没有看到下一个 VC 的手势识别器处于活动状态...

提前感谢有人可能提供的任何见解!

【问题讨论】:

  • 如果你解决了这个问题,请告诉我,我现在遇到了同样的问题
  • 这完全有可能,因为我看到它在某些应用程序中工作,但我不知道如何处理它

标签: ios objective-c uikit uigesturerecognizer uitouch


【解决方案1】:

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

在故事板中,我将 UINavigationController 设置为初始视图控制器。

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

#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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    相关资源
    最近更新 更多