动画 UIScrollView contentInset 导致跳转卡顿

2024-01-11

我实现了一个自定义刷新控件(我自己的类,而不是子类),并且由于某种原因,自从迁移到 iOS 8 以来,设置滚动视图(特别是 UICollectionView)的 contentInset 来启动刷新动画会导致奇怪的跳跃/卡顿。这是我的代码:

- (void)containingScrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat scrollPosition = scrollView.contentOffset.y + scrollView.contentInset.top;

    if( scrollPosition > 0 || self.isRefreshing )
    {
        return;
    }

    CGFloat percentWidth = fabs( scrollPosition ) / self.frame.size.height / 2;

    CGRect maskFrame = self.maskLayer.frame;

    maskFrame.size.width = self.imageLayer.frame.size.width * percentWidth;

    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
    self.maskLayer.frame = maskFrame;
    [CATransaction commit];
}

- (void)containingScrollViewDidEndDragging:(UIScrollView *)scrollView
{
    if( ( self.maskLayer.frame.size.width >= self.imageLayer.frame.size.width ) && !self.isRefreshing )
    {
        self.isRefreshing = YES;
        [self setLoadingScrollViewInsets:scrollView];
        [self startAnimation];
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
}

- (void)setLoadingScrollViewInsets:(UIScrollView *)scrollView
{
    UIEdgeInsets loadingInset = scrollView.contentInset;
    loadingInset.top += self.frame.size.height;

    UIViewAnimationOptions options = UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState;

    [UIView animateWithDuration:0.2 delay:0 options:options animations:^
    {
        scrollView.contentInset = loadingInset;
    }
    completion:nil];
}

基本上,一旦用户释放刷新,我就会将 contentInset 动画设置到刷新控件的高度。我认为动画会减少卡顿/跳跃,这在 iOS 7 中做到了。但在 iOS 8 中,当滚动视图从拖动中释放时,滚动视图内容实际上从释放点向下跳跃,而不是仅仅对 contentInset 进行动画处理很快,然后动画就顺利起来了。我不确定这是 iOS 8 中的错误还是什么。我也尝试添加:

scrollView.contentOffset = CGPointZero;

在动画块中,这没有改变任何东西。

有人有什么想法吗?任何帮助将不胜感激。谢谢!


我将动画块的方法更改为:

- (void)setLoadingScrollViewInsets:(UIScrollView *)scrollView
{
    UIEdgeInsets loadingInset = scrollView.contentInset;
    loadingInset.top += self.view.frame.size.height;

    CGPoint contentOffset = scrollView.contentOffset;

    [UIView animateWithDuration:0.2 animations:^
    {
        scrollView.contentInset = loadingInset;
        scrollView.contentOffset = contentOffset;
    }];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

动画 UIScrollView contentInset 导致跳转卡顿 的相关文章

随机推荐