UIPageViewController阻止使用viewcontrollers滚动

时间:2014-06-02 02:14:49

标签: ios objective-c uitableview uipageviewcontroller

我有一个UIViewController,其UIPageViewController属性。我遇到的问题是当viewcontroller正在拖动时(uitableview / uitableviewcell),pageviewcontroller滑动接管并在其childrencontroller视图中滚动时更改页面。

我尝试添加一个手势并检查触摸是否为UITableViewCell以防止手势触摸,但它无效。

我正在尝试实现类似的感觉,当您从左向右滑动时,Twitter或Snapchat应用程序会显示新的viewcontroller视图。

#import "ContainerViewController.h"

@interface ContainerViewController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIPageViewController *pageViewController;

@end

@implementation ContainerViewController

- (id)initWithViewControllers:(NSArray *)viewControllers
{
    self = [super init];
    if (self) {
        self.viewControllers = viewControllers;
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.pageViewController.view];
}

#pragma mark - Accessors

- (UIPageViewController *)pageViewController
{
    if (_pageViewController) {
        return _pageViewController;
    }

    _pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
                                                          navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                                                                        options:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.0f] forKey:UIPageViewControllerOptionInterPageSpacingKey]];
    _pageViewController.dataSource = self;
    _pageViewController.delegate = self;

    for (UIView *view in _pageViewController.view.subviews) {
        if ([view isKindOfClass:[UIScrollView class]]) {

            UIPanGestureRecognizer *g = [[UIPanGestureRecognizer alloc] initWithTarget:self action:nil];
            g.delegate = self;

            UIScrollView *scrollView = (UIScrollView *)view;
            [scrollView addGestureRecognizer:g];
        }
    }

    [_pageViewController setViewControllers:@[self.viewControllers[1]]
                                  direction:UIPageViewControllerNavigationDirectionForward
                                   animated:NO
                                 completion:nil];

    return _pageViewController;
}

#pragma mark - UIPageViewControllerDataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = [self.viewControllers indexOfObject:viewController];
    if (index == 0) {
        return nil;
    }

    return self.viewControllers[index - 1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = [self.viewControllers indexOfObject:viewController];
    if (index >= self.viewControllers.count - 1) {
        return nil;
    }

    return self.viewControllers[index + 1];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{   
    // UITableViewCellContentView => UITableViewCellScrollView => UITableViewCell
    if([touch.view.superview.superview isKindOfClass:[UITableViewCell class]]) {
        NSLog(@"PREVENT SCROLLL");
        return NO;
    }

    return YES;
}

@end

0 个答案:

没有答案