2013-06-06 82 views
0

我現在正在爲kids創建一個應用程序。此應用程序使用「UIPageViewController」。孩子們可以通過拖動手指在頁面內繪製線條。在UIPageViewController上方繪製一條路徑

問題是,當在頁面上方拖動頁面時翻頁:我如何禁止某個區域的翻頁動作,以便孩子們可以在那裏畫線?

+0

爲什麼你需要一個UIPageViewController擺在首位?難道你不能使用正常的UIViewController並且在適當的地方添加UIGestureRecognizers來自己照顧分頁嗎? – TheEye

回答

1

添加自來水UITapGestureRecognizer在viewDidLoad中

/** 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; 
}