0

我现在正在为孩子们创建一个应用程序。这个应用程序使用“UIPageViewController”。孩子们可以通过拖动手指在页面内画线。

问题是,当在页面上方拖动时,该页面被翻转:我如何禁用特定区域的页面翻转操作,以便孩子们可以在那里画线?

4

1 回答 1

1

在 viewDidLoad 中添加点击 UITapGestureRecognizer

/** add gesture recognizier */
UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
singleTap.numberOfTouchesRequired = 1;
singleTap.cancelsTouchesInView    = NO;
singleTap.delegate                = self;
[_pageController.view addGestureRecognizer:singleTap];
[singleTap release];

捕获 UITapGestureRecognizer 委托方法。

/** recognized tap on pageviewcontroller */
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:    (UITouch *)touch
{
    CGPoint touchPoint   = [touch locationInView:self.view];
    /** detect screen resolution */
    CGRect  screenBounds = [UIScreen mainScreen].bounds;

    if(touchPoint.x > (screenBounds.size.width *0.15) && touchPoint.x <     (screenBounds.size.width *0.75))
    {
        /** tap is on center */
        canScroll = NO;
    } else {
        /** tap is on corners */
        canScroll = YES;
    }

    /** detect the tap view */
    UIView *view = [self.view hitTest:touchPoint withEvent:nil];
    if([view isKindOfClass:[UIButton class]])
    {
        canScroll = NO;
    }

    return NO;
}

覆盖 UIPageViewController 数据源方法

/** move to previous page */
- (UIViewController *) pageViewController: (UIPageViewController *)pageViewController     viewControllerBeforeViewController:(UIViewController *)viewController
{
    if(canScroll)
    {
        return _prevPageViewController;
    }
    return  nil;
}

/** move to next page */
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
       viewControllerAfterViewController:(UIViewController *)viewController
{
    if(canScroll)
    { 
        return _nextPageViewController;
    }
    return nil;
}
于 2013-06-06T09:28:15.960 回答