【问题标题】:SWRevealViewController panGestureRecognizer with pageViewController iOS(side menu on swipe)SWRevealViewController panGestureRecognizer 与 pageViewController iOS(滑动侧菜单)
【发布时间】:2016-01-27 20:41:15
【问题描述】:
  1. 如果我在 frontViewController(在我的情况下是 HomescreenViewController)中已经有一些其他滑动手势,我如何使用 panGestureRecognizer 打开 sideMenu? 我的 frontViewController 包含一个有 5 个页面的 pageViewController。因此 pageViewController 已经启用了向左滑动和向右滑动。

    我在frontViewController的viewDidLoad中使用了如下代码:

    _sideButton.target = self.revealViewController;
    _sideButton.action = @selector(revealToggle:);
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    [self.view addGestureRecognizer:self.revealViewController.tapGestureRecognizer];
    

    这不会与 pageViewController 向右滑动一起完成。 然后我使用这段代码:

        self.revealViewController.draggableBorderWidth = self.view.frame.size.width / 2;
    

    但这也不起作用。我使用的是最新的 SWRevealViewController 2.4.0 版

    这是我的 frontViewController 的视图层次结构。 HomescreenViewController>pagerView>PageviewController>listingViewController

  2. 另外,当侧边菜单出现时,frontviewController 的部分处于活动状态,当我尝试通过向左滑动来关闭侧边菜单时,它会将其识别为 pageviewcontroller 滑动。我希望第一个问题的解决方案也能解决这个问题。

更新(答案): 查找问题 2 的已接受答案。 对于第一个问题,如果您遇到像我这样的问题,只需使用下面的代码创建一个左侧宽度为 20 像素的视图,并将 panGestureRecognizer 添加到此视图中(在viewWillAppear)。

 SWSwipeViewForPanGesture=[[UIView alloc]initWithFrame:CGRectMake(0, 0,20, self.view.frame.size.height)];
_SWSwipeViewForPanGesture.backgroundColor=[UIColor clearColor];
[self.view addSubview:_SWSwipeViewForPanGesture];
[_SWSwipeViewForPanGesture addGestureRecognizer:self.revealViewController.panGestureRecognizer];

另外,我必须在- (void)revealController:(SWRevealViewController *)revealController didMoveToPosition:(FrontViewPosition)position方法中添加以下代码这是一个委托方法。所以,你应该在viewdidload中确认委托。

    [_SWSwipeViewForPanGesture addGestureRecognizer:self.revealViewController.panGestureRecognizer];
[self.view bringSubviewToFront:_SWSwipeViewForPanGesture];

就是这样。它很简单!

【问题讨论】:

    标签: ios objective-c uipageviewcontroller uipangesturerecognizer swrevealviewcontroller


    【解决方案1】:

    第一个问题的解决方案:

    您无法在一个视图上识别两个滑动手势。所以基本上,回答你的第一个问题是不可能的。你不能在pageview 控制器中滑动SWRevealViewController

    第二个问题的解决方案:

    将此代码写入HomescreenViewControllerViewDidLoad方法中。

    - (void)viewDidLoad
    {
    SWRevealViewController *  revealController=[[SWRevealViewController alloc]init];
        revealController = [self revealViewController];
        [self.view addGestureRecognizer:revealController.panGestureRecognizer];
           revealController.delegate=self;
        [revealController panGestureRecognizer];
    
        [revealController tapGestureRecognizer];
        [btnSideMenu addTarget:revealController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
    
    }
    

    然后将SWRevealViewController 的这个委托方法添加到您的HomescreenViewController

    - (void)revealController:(SWRevealViewController *)revealController didMoveToPosition:(FrontViewPosition)position
    {
        if (revealController.frontViewPosition == FrontViewPositionRight)
        {
            UIView *lockingView = [UIView new];
            lockingView.translatesAutoresizingMaskIntoConstraints = NO;
    
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:revealController action:@selector(revealToggle:)];
            [lockingView addGestureRecognizer:tap];
            [lockingView addGestureRecognizer:revealController.panGestureRecognizer];
            [lockingView setTag:1000];
            [revealController.frontViewController.view addSubview:lockingView];
    
    
            lockingView.backgroundColor=[UIColor ClearColor];
    
            NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(lockingView);
    
            [revealController.frontViewController.view addConstraints:
             [NSLayoutConstraint constraintsWithVisualFormat:@"|[lockingView]|"
                                                     options:0
                                                     metrics:nil
                                                       views:viewsDictionary]];
            [revealController.frontViewController.view addConstraints:
             [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[lockingView]|"
                                                     options:0
                                                     metrics:nil
                                                       views:viewsDictionary]];
            [lockingView sizeToFit];
        }
        else
            [[revealController.frontViewController.view viewWithTag:1000] removeFromSuperview];
    }
    

    可能会,它会帮助你。

    【讨论】:

    • 感谢 Bhumica 的回答。第二个问题解决了。有没有办法在 index 为零时禁用 pageviewcontroller 上的 rightwipe?或者我们可以向HomescreenViewController 的左端并向其添加revealController.panGestureRecognizer?
    • 在 Ipad splitviewcontroller 中,我看到一个应用程序,当我们在索引 0 处时,滑动手势可以将菜单拉到前面。
    • @abhi1992 但是如果您将透明视图放在网页浏览的索引 0 上,那么您将如何滑动以转到下一页。?
    • 我的意思是在视图的左侧添加一个厚度为 20 像素的视图。
    • @abhi1992 如果在透明视图上向左滑动而不是打开菜单并且它会向右滑动而不是打开网页浏览的 1 索引,则做一件事在网页浏览的 o 索引上进行透明视图。
    猜你喜欢
    • 2015-10-28
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 2017-04-08
    • 2021-01-26
    • 1970-01-01
    相关资源
    最近更新 更多