Unable to setContentOffset in scrollViewDidEndDragging(无法在 scrollViewDidEndDragging 中设置 ContentOffset)
问题描述
我正在尝试在以下委托方法中修改滚动的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法在 scrollViewDidEndDragging 中设置 ContentOffset
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01