2012-06-14 23 views
1


我想阻止在我的UIScrollview的第3頁滾動和「劫持滑動」手勢觸發某事。其他。在這個動作之後,我想要反應式滾動。在UIScrollView劫持「滑動到下一頁」

這不起作用。

- (void)scrollViewDidScroll:(UIScrollView *)sender 
{ 
    if(scrollView.contentOffset.x == self.view.frame.size.width * 2 ) { 
     // disable scrolling 
     scrollView.scrollEnabled = NO; 
    } 
} 


// hijack the next scrolling event 
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 

此委託不叫當scrollEnabled = NO

感謝您的幫助


編輯事件處理IST不叫;-(

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Default background color 
    self.view.backgroundColor = [UIColor redColor]; 

    // Create scroll view 
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    scrollView.pagingEnabled = YES; 
    scrollView.showsHorizontalScrollIndicator = NO; 
    scrollView.showsVerticalScrollIndicator = NO; 
    scrollView.scrollsToTop = NO; 
    scrollView.delegate = self; 


    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 
    recognizer.direction = UISwipeGestureRecognizerDirectionLeft; 
    [scrollView addGestureRecognizer:recognizer]; 
    [recognizer release]; 
    [scrollView delaysContentTouches]; 

    // Create subviews (pages) 
    NSInteger numberOfViews = 4; 
    for (int i = 0; i < numberOfViews; i++) { 
     // x pos 
     CGFloat yOrigin = i * self.view.frame.size.width; 

     // Create subview and add to scrollView 
     UIView *pageView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
     pageView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1]; 

     [scrollView addSubview:pageView]; 
     [pageView release]; 
    } 

    // Set contentsize 
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height); 


    // Add scrollView to view and release 
    [self.view addSubview:scrollView]; 
    [scrollView release]; 

} 


-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { 
    NSLog(@"swipe!!!!"); 
    scrollView.scrollEnabled = YES; 
} 

回答

2

如果你disa竹葉提取滾動視圖:

scrollView.scrollEnabled = NO; 

是不可避免的委託方法不叫,所以你需要另一種方式來處理刷卡,而在劫持模式。有一兩件事你可以嘗試使用一個UISwipeGestureRecognizer:不是簡單地禁用滾動,你可以一個UISwipeGestureRecognizer來查看和關聯從處理方法處理刷卡:

UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 
recognizer.direction = UISwipeGestureRecognizerDirectionRight; 
[self.view addGestureRecognizer:recognizer]; 

handleSwipeFrom你會重新啓用滾動:

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { 
    // do your hijack here 
    scrollView.scrollEnabled = YES; 
} 
+0

我應該將GestureRecognizer添加到scollView還是添加到「SubView」之一。我的演示中沒有調用handleSwipeFrom。 – fabian

+0

我建議無論是對於scrollView還是其超級視圖...另外,不要忘記設置滑動的方向(請參閱我的編輯在幾秒鐘內)... – sergio

+0

仍然沒有工作(更新我的代碼) – fabian