无法在 scrollViewDidEndDragging 中设置 ContentOffset

Unable to setContentOffset in scrollViewDidEndDragging(无法在 scrollViewDidEndDragging 中设置 ContentOffset)
本文介绍了无法在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)