UIPageViewController 和 UISwipeGestureRecognizer

Posted

技术标签:

【中文标题】UIPageViewController 和 UISwipeGestureRecognizer【英文标题】:UIPageViewController ans UISwipeGestureRecognizer 【发布时间】:2014-07-01 18:38:28 【问题描述】:

我在UIPageViewController 上有几页。每个页面都是从其自己的视图创建的,该视图具有嵌套视图。其中之一是图像。我想以这种方式进行控制,该用户可以通过在除图像之外的所有内容上滑动来更改页面。在图像上滑动会改变图像而不是页面。

我怎样才能做到这一点?我已将UISwipeGestureRecognizer 添加到图像中,将 userInteraction 设置为 YES,但滑动发送通过并导致翻页。

我在视图加载方法中有这段代码(awakeFromNib)

UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeImageRight)];
[swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
swipeGestureRight.delegate = self;
swipeGestureRight.numberOfTouchesRequired = 1;
[self.image addGestureRecognizer:swipeGestureRight];

【问题讨论】:

您的滑动识别器必须“缩放层次结构”。也就是说,您的 imageview 是来自控制器的视图的子视图,其中包含页面控制器滑动。您必须告诉滑动(从页面控制器)仅在您自己失败后才响应。搜索要求手势识别器失败。 试试这个示例以获得一些参考github.com/jackhumphries/UIPageViewController-PDF 【参考方案1】:

这样的事情应该对你有用:

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    if (gestureRecognizer == self)
        if ([otherGestureRecognizer isMemberOfClass:self.class])
            if ([self isGestureRecognizerInSuperviewHierarchy:otherGestureRecognizer])
                return YES;
             else if ([self isGestureRecognizerInSiblings:otherGestureRecognizer])
                return YES;
            
        
    
    return NO;


- (BOOL) isGestureRecognizerInSiblings:(UIGestureRecognizer *)recognizer
    UIView *superview = self.view.superview;
    NSUInteger index = [superview.subviews indexOfObject:self.view];
    if (index != NSNotFound)
    for (int i = 0; i < index; i++)
            UIView *sibling = superview.subviews[i];
            for (UIGestureRecognizer *viewRecognizer in sibling.gestureRecognizers)
                if (recognizer == viewRecognizer)
                    return YES;
                
            
        
    
    return NO;

- (BOOL) isGestureRecognizerInSuperviewHierarchy:(UIGestureRecognizer *)recognizer
    if (!recognizer) return NO;
    if (!self.view) return NO;
    //Check siblings
    UIView *superview = self.view;
    while (YES) 
        superview = superview.superview;
        if (!superview) return NO;
        for (UIGestureRecognizer *viewRecognizer in superview.gestureRecognizers)
            if (recognizer == viewRecognizer)
                return YES;
            
        
    


- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    if ([gestureRecognizer respondsToSelector:@selector(gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer:)])
        return [(id <UIGestureRecognizerDelegate>)gestureRecognizer gestureRecognizer:gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:otherGestureRecognizer];
    
    return NO;

【讨论】:

它不工作.. otherGestureRecognizer 从未设置为 UIPageViewController 手势。

以上是关于UIPageViewController 和 UISwipeGestureRecognizer的主要内容,如果未能解决你的问题,请参考以下文章

Swift pagecontroller 故事板 UI 对象与 UIPageviewcontroller 连接

UIPageViewController -presentationCount 不触发

如何更改 UIPageViewController 中的按钮名称

iOS6 自动旋转 - UIPageViewController

如何在 UIPageViewController 中重用相同的 ViewController?

更改 UIPageViewController 中显示的初始视图控制器