【问题标题】:Tap on ScrollView calls `scrollViewWillBeginDecelerating` when the scrollView is set pagingEnabled to YES当 scrollView 设置 pagingEnabled 为 YES 时,点击 ScrollView 调用`scrollViewWillBeginDecelerating`
【发布时间】:2017-04-04 13:02:49
【问题描述】:

当我点击 ScrollView 时,它会调用方法 scrollViewWillBeginDecelerating 和 scrollViewDidEndDecelerating,即使在将 scrollView's 设置为 pagingEnabled 到 YES 之后也是如此

我有scrollView 和subViews 并尝试根据subViews 设置contentOffset。但它不起作用。

这是我的代码:

self.myScrollView.frame = CGRectMake(0, 0, pageWidth, pageHeight);
self.myScrollView.contentSize = CGSizeMake(pageWidth * self.contentViewControllers.count, pageHeight);

for (NSInteger i = 0; i < self.contentViewControllers.count; i++) {
    UILabel *label = [UILabel new];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = [NSString stringWithFormat:@"Page %ld",i];
    label.backgroundColor = [UIColor randomColor];
    label.frame = CGRectMake(i * pageWidth, 0, pageWidth, pageHeight);
    [self.myScrollView addSubview:label];
}

self.myScrollView.contentInset = UIEdgeInsetsMake(120, 0, 0, 0);
self.myScrollView.contentOffset = CGPointMake(0, - 120);
self.myScrollView.scrollIndicatorInsets = UIEdgeInsetsMake(120, 0, 0, 0);

- (UIScrollView *)myScrollView{
if (!_myScrollView) {
    _myScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    _myScrollView.backgroundColor = [UIColor blackColor];
    _myScrollView.pagingEnabled = YES;
    _myScrollView.directionalLockEnabled = YES;
    _myScrollView.delegate = self;
    //_myScrollView.delaysContentTouches = NO;
    //_myScrollView.scrollsToTop = NO;
}

return _myScrollView;
}

当我点击scrollView 中的任何subView 时,它会调用委托方法:

scrollViewWillBeginDecelerating >> scrollViewDidScroll >> scrollViewDidEndDecelerating

而contentOffset 更改为{0,0}

【问题讨论】:

    标签: ios objective-c uiscrollview uiscrollviewdelegate


    【解决方案1】:

    请像这样更新您的代码:

    请在 for 循环后设置 contentSize。

    self.myScrollView.frame = CGRectMake(0, 0, pageWidth, pageHeight);
    
    for (NSInteger i = 0; i < self.contentViewControllers.count; i++) {
        UILabel *label = [UILabel new];
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = [NSString stringWithFormat:@"Page %ld",i];
        label.backgroundColor = [UIColor randomColor];
        label.frame = CGRectMake(i * pageWidth, 0, pageWidth, pageHeight);
        [self.myScrollView addSubview:label];
    }
    
    self.myScrollView.contentInset = UIEdgeInsetsMake(120, 0, 0, 0);
    self.myScrollView.contentOffset = CGPointMake(0, - 120);
    self.myScrollView.scrollIndicatorInsets = UIEdgeInsetsMake(120, 0, 0, 0);
    
    self.myScrollView.contentSize = CGSizeMake(pageWidth * self.contentViewControllers.count, pageHeight);
    
    - (UIScrollView *)myScrollView{
    if (!_myScrollView) {
        _myScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _myScrollView.backgroundColor = [UIColor blackColor];
        _myScrollView.pagingEnabled = YES;
        _myScrollView.directionalLockEnabled = YES;
        _myScrollView.delegate = self;
        //_myScrollView.delaysContentTouches = NO;
        //_myScrollView.scrollsToTop = NO;
    }
    
    return _myScrollView;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-23
      相关资源
      最近更新 更多