问题描述
我正在尝试在以下委托方法中修改滚动的 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 中设置 ContentOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!