首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义UIPageViewController手势识别器滚动行为

自定义UIPageViewController手势识别器滚动行为
EN

Stack Overflow用户
提问于 2014-02-21 07:26:42
回答 1查看 5.3K关注 0票数 5

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

代码语言:javascript
复制
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手势,并且需要从两个按钮滚动页面视图控制器,就像用户尝试使用pan一样,它的起点在我的buttons框架内,否则页面视图控制器就不应该滚动。

我使用的是UIPageViewControllerTransitionStyleScroll. transitionStyle

任何解决办法..。提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-11 05:46:09

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

我所做的,首先得到了一个属性中的property控制器scorll视图。

代码语言:javascript
复制
for (UIView *view in mypageviewcontroller.view.subviews) {
  if([view isKindOfClass:[UIScrollView class]])
  {
      pagescrollview= (UIScrollView *)view;
  }
}

然后将pan手势分配给页面视图控制器、藐视视图和手势委托。

代码语言:javascript
复制
UIPanGestureRecognizer* g1 = [[UIPanGestureRecognizer alloc] initWithTarget:self                                                       action:@selector(gesture)];

[g1 setDelegate:self];

[pagescrollview addGestureRecognizer:g1];

然后在手势委托中,我检查手势是否从我的愿望点开始,如果从应该滚动页面视图控制器的位置开始,则该委托方法应该返回no以启用原始的分页视图手势来接收pan手势。

代码语言:javascript
复制
-(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;
}

希望它能帮到别人。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21927970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档