我想防止在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 时不调用此委托

感谢您的帮助

编辑 EventHandler 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;
}

最佳答案

如果禁用滚动视图:

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

09-11 20:16