【问题标题】:Customizing UIPageViewController gesture recognizers Scroll behavior自定义 UIPageViewController 手势识别器滚动行为
【发布时间】:2014-02-21 07:26:42
【问题描述】:

我想在 pageviewcontroller.m 文件中实现-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch gesture delegate,但我无法获得任何手势:

NSArray *temp = self.gestureRecognizers;
NSLog(@"count %i",temp.count); // this logs count 0 

NSArray *temp = self.view.gestureRecognizers;
NSLog(@"count %i",temp.count);  // this also logs count 0 

for (UIGestureRecognizer *gR in temp) {
    gR.delegate = self;
}

在上面的代码中,self指向pageviewcontroller。

因此我无法将委托分配给 pageviewcontroller 手势。

编辑部分:

好的,我明白了,由于 uipageviewscroll 样式,我没有得到任何手势对象。

但我有一个问题,我需要禁用 pageviewcontroller pan 手势,并且需要从两个按钮滚动 pageviewcontroller,就像用户尝试平移一样,它的起点在我的 uibuttons 框架内,然后是 pageviewcontroller否则不应该滚动。

我正在使用 transitionStyle UIPageViewControllerTransitionStyleScroll

任何解决方案... 提前致谢

【问题讨论】:

    标签: ios uikit uipageviewcontroller


    【解决方案1】:

    在尝试了这么多黑客之后,我终于找到了解决方案。

    我所做的,首先在一个属性中得到了pageviewcontroller scorll view

    for (UIView *view in mypageviewcontroller.view.subviews) {
      if([view isKindOfClass:[UIScrollView class]])
      {
          pagescrollview= (UIScrollView *)view;
      }
    }
    

    然后将平移手势分配给 pageviewcontroller scorllview 和手势委托。

    UIPanGestureRecognizer* g1 = [[UIPanGestureRecognizer alloc] initWithTarget:self                                                       action:@selector(gesture)];
    
    [g1 setDelegate:self];
    
    [pagescrollview addGestureRecognizer:g1];
    

    然后在手势委托中,我检查了手势是否从我想要的点开始,如果从应该滚动 pageviewcontroller 的位置开始,那么这个委托方法应该返回 no 以启用原始 pagescorllview 手势来接收平移手势。

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
    {
      if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
    
        CGPoint touchPoint = [touch locationInView:self.view];
        if(floor(NSFoundationVersionNumber)<=NSFoundationVersionNumber_iOS_6_1)
            touchPoint.y -=44;
        else
            touchPoint.y -=64;
    
        PGNavigationController *nav =ViewControllerArray[VisiblePageindex];
        PageContentVC *pagecontentvc = ((PageContentVC *) nav.visibleViewController);
    
        if (touchPoint.y > pagecontentvc.leftpanalbtn.frame.origin.y && (pagecontentvc.leftpanalbtn.frame.size.height+pagecontentvc.leftpanalbtn.frame.origin.y )>touchPoint.y && touchPoint.x >pagecontentvc.leftpanalbtn.frame.origin.x
            && touchPoint.x<(pagecontentvc.leftpanalbtn.frame.origin.x+pagecontentvc.leftpanalbtn.frame.size.width)) {
            return NO;
        }
        else if (touchPoint.y > pagecontentvc.rightpanalbtn.frame.origin.y && (pagecontentvc.rightpanalbtn.frame.size.height+pagecontentvc.rightpanalbtn.frame.origin.y )>touchPoint.y && touchPoint.x >pagecontentvc.rightpanalbtn.frame.origin.x
                 && touchPoint.x<(pagecontentvc.rightpanalbtn.frame.origin.x+pagecontentvc.rightpanalbtn.frame.size.width))
        {
    
            return NO;
        }
        if( touchPoint.y>282 && touchPoint.x>118 &&touchPoint.y<282+75 && touchPoint.x < 118+85)
        {
            return NO;
        }
        // else if()
      }
      return YES;
    }
    

    希望对其他人有所帮助。

    【讨论】:

    • @selector(gesture) 在哪里,它有什么作用?
    猜你喜欢
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    相关资源
    最近更新 更多