Unable to setContentOffset in scrollViewDidEndDragging

南楼画角 提交于 2019-12-01 20:16:42

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];
    });
}

}

and it actually works :-)

You should "nil" your scrollView delegate before animation, set content offset in scrollViewDidEndDragging:willDecelerate: will cause repeat delegation.

self.scrollView.delegate = nil;
[self.scrollView setContentOffset:CGPointMake(x, y) animated:YES];
self.scrollView.delegate = self;

Depending on the effect you are trying to achieve, you may have more luck with this delegate method:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset

You can choose what target content offset you want to use for the scroll view to decelerate to once the user stops dragging.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!