无法在scrollViewDidEndDragging中设置setContentOffset [英] Unable to setContentOffset in scrollViewDidEndDragging

查看:742
本文介绍了无法在scrollViewDidEndDragging中设置setContentOffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在以下委托方法中修改我的滚动的contentOffset:

I am attempting to modify the contentOffset of my scroll within the following delegate method:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

我尝试了以下两种方法:

I have tried both of the following:

[UIView animateWithDuration:.2 animations:^ {
   [scrollView setContentOffset:CGPointZero animated:NO];
}];

[UIView animateWithDuration:.2 animations:^ {
   CGRect svBounds = self.bounds;
   svBounds.origin.y = 0;
   self.bounds = svBounds; 
}];

问题在于,虽然这会立即改变偏移(在动画完成后通过日志证明) ,它不会改变可见的滚动位置。我的滚动视图的后续委托方法进一步证明了这一点,它表明边界确实没有改变。意味着y位置不为0.

The problem is that although this changes the offset right away (proven by logs after the animation is complete), it doesn't change the visible scroll location. This is further proven by subsequent delegate methods of my scroll view which indicate that the bounds have indeed not changed at all. Meaning that the y location is not 0.

是否禁止在此特定委托方法中更改内容偏移量?如果是这样,我什么时候可以改变偏移?我正在尝试执行动画,一旦用户完成拖动(在一定数量之后),将可见滚动区域返回到顶部。

Is it forbidden to change the content offset in this particular delegate method? If so, when is it possible for me to change the offset? I'm trying to perform an animation of returning the visible scroll area to the top once the user finishes dragging (after a certain amount).

谢谢!

推荐答案

我最终做的是再次调度到主队列。
含义:

what i did eventually is dispatching again to the main queue. meaning:

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
NSLog(@"END DRAG");
CGFloat yVelocity = [scrollView.panGestureRecognizer velocityInView:scrollView].y;
if (yVelocity < 0) {
    NSLog(@"Up");
} else {
    NSLog(@"Down");
}

if (yVelocity < 0 && scrollView.contentOffset.y < -100 && scrollView.contentOffset.y > -200) //UP - Hide
{
    NSLog(@"hiding");
    dispatch_async(dispatch_get_main_queue(), ^{
        [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    });
}
else if (yVelocity > 0 && scrollView.contentOffset.y < -100) //DOWN - Show
{
    NSLog(@"showing");
    dispatch_async(dispatch_get_main_queue(), ^{
        [scrollView setContentOffset:CGPointMake(0, -300) animated:YES];
    });
}

}

它确实有效:-)

这篇关于无法在scrollViewDidEndDragging中设置setContentOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆