objective-c - 在 UIScrollView 中劫持 "swipe to next page"

标签 objective-c ios uiscrollview


我想防止在我的 UIScrollview 的第 3 页上滚动和“劫持滑动”手势来触发某事。别的。完成此操作后,我想响应式滚动。

这行不通。

- (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 时不调用此委托(delegate)

感谢帮助


编辑 EventHandler 未被调用 ;-(

- (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 :

    scrollView.scrollEnabled = NO;

不调用委托(delegate)方法是不可避免的,因此您需要一种替代方法来处理劫持模式下的滑动。您可以尝试的一件事是使用 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;
}

关于objective-c - 在 UIScrollView 中劫持 "swipe to next page",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11036192/

相关文章:

ios - ScrollView 在所有 iPhone、Swift 上不可用

ios - iOS 上类似 ORM 的功能

iOS - 将 NSDate 与另一个 NSDate 进行比较

objective-c - 从 PDF 流上的 TJ 回调生成的 CGPDFArray 中复制 CGPDFStrings

ios - UITableViewCell 内自定义 UIView 的 drawRect 的错误行为

iphone - 使两个 UIScrollViews 跟随彼此滚动

ios - 混合语言框架

iOS:动画不符合预期

ios - UIScrollView 高度和宽度

iphone - 将触摸手势发送到 UIScrollView 的 UIImageView subview