【问题标题】:QLPreviewController's viewQLPreviewController 的视图
【发布时间】:2011-05-22 21:50:09
【问题描述】:

我只是想访问 QLPreviewController.view。事实上,我想在其视图上捕捉一个点击事件以显示/隐藏工具栏等。我正在尝试:

QLPreviewController* qlpc = [QLPreviewController new];
    qlpc.delegate = self;
    qlpc.dataSource = self;
    qlpc.currentPreviewItemIndex=qlIndex;
    [navigator pushViewController:qlpc animated:YES];
    qlpc.title = [path lastPathComponent];
    [qlpc setToolbarItems:[NSArray arrayWithObjects:self.dirBrowserButton,self.space, self.editButton, self.btnSend, nil] animated:YES];
    UITapGestureRecognizer* gestTap  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showControls:)];
    gestTap.cancelsTouchesInView=NO;
    [qlpc.view addGestureRecognizer:[gestTap autorelease]];
    [qlpc release];

什么也没发生

如果我将 UITapRecognizer 附加到 navigationController.view,它只会在我触摸工具栏/导航栏时触发。 UISwipeGestureRecognizer 在这种情况下可以正常工作。

我尝试附加一个透明覆盖视图并在其上添加手势识别器,但没有运气。 好吧,我看到一些应用程序实现了这样的功能,很明显这是可能的,但是怎么做呢? 抱歉,我搜索了一整天,没有找到任何解决方案。请帮我。

【问题讨论】:

    标签: iphone ios qlpreviewcontroller


    【解决方案1】:

    使用您的解决方案,QLPreviewController 的视图是否仍会收到触摸?我尝试做类似的事情(我正在从 QLPreviewController 窃取视图以使用它),看起来我的叠加视图不会让任何东西通过它背后的视图。

    【讨论】:

    • 不。 QLPreviewController 和它的视图什么都没有。
    【解决方案2】:

    我今天一直在解决这个问题,覆盖 -(void)contentWasTappedInPreviewContentController:(id)item {} 的建议很接近,但是当你搞砸了预览控制器的处理时。

    所以我停止覆盖该方法,而是创建了一个 RAC 信号,该信号在调用该方法时触发。这不会影响 QL 的默认行为。我在 QLPreviewController 的子类中执行此操作,但这不是必需的。

    我的班级有一个财产:

     @property RACSignal *contentTapped;
    

    然后在我的 QLPreviewController 子类的 init 方法中:

    _contentTapped = [self   rac_signalForSelector:@selector(contentWasTappedInPreviewContentController:)];
    

    现在在另一个班级甚至内部你可以像这样使用信号:

    previewController.contentTapped subscribeNext:^(id x) {
        // Put your handler here!
    }];
    

    【讨论】:

    • 遗憾的是,尽管上述解决方案完成了我需要做的所有事情,但由于使用了不被视为“官方 api 方法”的“contentWasTappedInPreviewContentController”方法,Apple 不会接受提交。
    • 只是隐藏该方法... ;)
    【解决方案3】:

    这是我的解决方案(使用 KVO),我在其中监控导航栏状态 - 并在需要时显示工具栏(似乎在点击时它会自行隐藏工具栏)

    #define kNavigationBarKeyPath @"navigationBar.hidden"
    
    static void * const NavigationBarKVOContext = (void*)&NavigationBarKVOContext;
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        [self.navigationController setToolbarHidden:NO];
        [self.navigationController addObserver:self forKeyPath:kNavigationBarKeyPath options:NSKeyValueObservingOptionPrior context:NavigationBarKVOContext];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    
        [self.navigationController removeObserver:self forKeyPath:kNavigationBarKeyPath];
    }
    

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        if ( context == NavigationBarKVOContext ) {
            BOOL prior = [change[NSKeyValueChangeNotificationIsPriorKey] boolValue];
            if ( prior && self.navigationController.toolbarHidden ) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.navigationController setToolbarHidden:NO animated:YES];
                });
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      我发现这里没有一个可行的答案,但对我来说是子类化 QLPreviewController 并像这样覆盖 viewDidAppear:

      - (void)viewDidAppear:(BOOL)animated
      { 
          UITapGestureRecognizer *gestTap  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showControls:)];
          gestTap.cancelsTouchesInView = NO;
          [self.view addGestureRecognizer:[gestTap autorelease]];
      }
      

      【讨论】:

        【解决方案5】:

        好的,解决方法很简单。 刚刚在 keyWindow 上添加了一个覆盖视图。将手势识别器附加到叠加层上就可以了。

                QLPreviewController* qlpc = [QLPreviewController new];
            qlpc.delegate = self;
            qlpc.dataSource = self;
            qlpc.currentPreviewItemIndex=qlIndex;
            [navigator pushViewController:qlpc animated:YES];
            qlpc.title = [path lastPathComponent];
            UIView* overlay = [[[UIView alloc] initWithFrame:navigator.view.bounds] autorelease];
            [[[UIApplication sharedApplication] keyWindow] addSubview:overlay];
            [overlay setNeedsDisplay];
            [qlpc setToolbarItems:[NSArray arrayWithObjects:self.dirBrowserButton,self.space, self.editButton, self.btnSend, nil] animated:YES];
            UITapGestureRecognizer* gestTap  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showControls:)];
            gestTap.cancelsTouchesInView=NO;
            [overlay addGestureRecognizer:[gestTap autorelease]];
            [qlpc release];
        

        【讨论】:

          【解决方案6】:

          子类 QLPreviewController 然后覆盖

          -(void)contentWasTappedInPreviewContentController:(id)item {}

          就是这样!

          【讨论】:

          • 该委托在 iOS 上不可用
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多