ios - UIScrollView的 "pull up to load more"和 “pull down to go back"导致闪烁(附示例代码)

标签 ios objective-c uitableview uiviewcontroller uiscrollview

我正在研究一个 UI 效果,“上拉加载更多”和“下拉返回”。

它应该是这样工作的:当用户将 UIScrollView 向上拉一定距离时,会触发加载更多操作,UIScrollView 的下半部分 内容将是滚动到可见区域。之后,如果用户将 scrollView 下拉一段距离,它应该回滚到上半部分。

我通过设置 UIScrollViewcontentInsets 来实现它,当拖动触发 Action 时。

问题是在更改scrollView 的 contentInsets 时,它会闪烁。打印了一些日志,发现是contentInsets变化时contentOffset.y异常跳转造成的!但我不知道为什么。

以下是经过简化但足以重现问题的 ViewController 代码。通过日志,您可以清楚地看到发生了什么。

有人可以帮我解决吗?

#import "IssueShowcaseViewController.h"

@interface IssueShowcaseViewController () <UIScrollViewDelegate>
{
    BOOL _isShowingUpperView;
}

@property (nonatomic, strong) UIScrollView* scrollView;

@property (nonatomic, strong) UIView* upperView;
@property (nonatomic, strong) UIView* lowerView;

@end

@implementation IssueShowcaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.upperView = [[UIView alloc] initWithFrame:self.scrollView.bounds];
    self.lowerView = [[UIView alloc] initWithFrame:CGRectOffset(self.scrollView.bounds, 0, self.scrollView.bounds.size.height)];
    [self.upperView setBackgroundColor:[UIColor blueColor]];
    [self.lowerView setBackgroundColor:[UIColor greenColor]];

    //Add upperView to upper half, and lowerView to lower half:
    [self.scrollView addSubview:self.upperView];
    [self.scrollView addSubview:self.lowerView];
    [self.view addSubview:self.scrollView];

    _isShowingUpperView = YES;

    self.scrollView.delegate = self;
    // Observe scrollView.contentOffset for watching unnormal changes:
    [self.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

    // Set contentSize as the sum height of two subviews:
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(self.lowerView.frame) - CGRectGetMinY(self.upperView.frame));
    // But the contentInset is set as to not allow scrolling into lower half:
    // 1-pixel more space is necessary, as the scrollView would not be able to scroll downwards without it:
    self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 1-self.lowerView.frame.size.height, 0);
}

- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (_isShowingUpperView)
    {
        if (scrollView.contentOffset.y > 60)
        {
            scrollView.contentInset = UIEdgeInsetsMake(1-self.upperView.frame.size.height, 0, 0, 0);
            _isShowingUpperView = NO;
        }
    }
    else
    {
        if (scrollView.contentOffset.y < self.upperView.frame.size.height - 60)
        {
            scrollView.contentInset = UIEdgeInsetsMake(0, 0, 1-self.lowerView.frame.size.height, 0);
            _isShowingUpperView = YES;
        }
    }
}

#pragma mark    Here we can see what happens:
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"contentOffsetY = %f", scrollView.contentOffset.y);
}

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (object == self.scrollView) {
        CGPoint oldOffset = [[change objectForKey:@"old"] CGPointValue];
        CGPoint newOffset = [[change objectForKey:@"new"] CGPointValue];
        if (fabs(newOffset.y - oldOffset.y) > 300) {
            NSLog(@"Weird: %@", change);
        }
    }
}

@end

最佳答案

经过多次尝试,我找到了解决方案:首先在动画 block 中设置零 contentInsets,然后在完成 block 中设置新的 contentInsets 并更正 contentOffset:

- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (_isShowingUpperView)
{
    if (scrollView.contentOffset.y > 60)
    {
        [UIView animateWithDuration:0.0f animations:^{
            scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3f animations:^{
                scrollView.contentOffset = CGPointMake(0, self.upperView.frame.size.height);
            } completion:^(BOOL finished) {
                scrollView.contentInset = UIEdgeInsetsMake(1-self.upperView.frame.size.height, 0, 0, 0);
            }];
        }];
        _isShowingUpperView = NO;
    }
}
else
{
    if (scrollView.contentOffset.y < self.upperView.frame.size.height - 60)
    {
        [UIView animateWithDuration:0.0f animations:^{
            scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3f animations:^{
                scrollView.contentOffset = CGPointMake(0, 0);
            } completion:^(BOOL finished) {
                scrollView.contentInset = UIEdgeInsetsMake(0, 0, 1-self.lowerView.frame.size.height, 0);
            }];
        }];
        _isShowingUpperView = YES;
    }
}
}

关于ios - UIScrollView的 "pull up to load more"和 “pull down to go back"导致闪烁(附示例代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30340342/

相关文章:

ios - 为什么我们没有 UIBarMetricsLandscapePad?

ios - Google 登录在 iOS 中不起作用 - 无效的 GOOGLE_APP_ID 错误

ios - Foundation 集合无法存储 nil 是否有深层设计原因?

ios - 使 UITableView 看起来像 iPhone 中的 UIPopover(但没有箭头)

uitableview - 我可以在同一个 View Controller 中有一个 CollectionView 和一个 TableView

ios - 无法将类型 'UIImage?' 的值分配给 swift 3 中的类型 'NSData?'

ios - hoSTLess XCTest 目标能否收集代码覆盖率数据?

ios - 如何为 iphone 5 和 iphone 6 创建不同的约束

ios - iAds(甚至是苹果示例中的 iAd 套件)停止出现在我的 iPhone 上,但它们在模拟器上运行

ios - 如何在再次单击表格 View 时停止歌曲并在单击另一个单元格时播放另一首歌曲