问题描述
简而言之,我需要确切知道滚动视图何时停止滚动。通过'停止滚动',我的意思是它不再移动而不被触摸的那一刻。
In short, I need to know exactly when the scrollview stopped scrolling. By 'stopped scrolling', I mean the moment at which it is no longer moving and not being touched.
我一直在处理一个水平的UIScrollView子类(适用于iOS) 4)其中包含选择标签。它的一个要求是它停止滚动到某个速度以下,以便更快地进行用户交互。它还应该捕捉到选项卡的开头。换句话说,当用户释放滚动视图并且其速度较低时,它会捕捉到一个位置。我已经实现了这个并且它有效,但它有一个错误。
I've been working on a horizontal UIScrollView subclass (for iOS 4) with selection tabs in it. One of its requirements is that it stops scrolling below a certain speed to allow user interaction more quickly. It should also snap to the start of a tab. In other words, when the user releases the scrollview and its speed is low, it snaps to a position. I've implemented this and it works, but there's a bug in it.
我现在拥有的:
scrollview是它自己的委托。每次调用scrollViewDidScroll:时,它都会刷新与速度相关的变量:
The scrollview is its own delegate. at every call to scrollViewDidScroll:, it refreshes its speed-related variables:
-(void)refreshCurrentSpeed
{
float currentOffset = self.contentOffset.x;
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
deltaOffset = (currentOffset - prevOffset);
deltaTime = (currentTime - prevTime);
currentSpeed = deltaOffset/deltaTime;
prevOffset = currentOffset;
prevTime = currentTime;
NSLog(@"deltaOffset is now %f, deltaTime is now %f and speed is %f",deltaOffset,deltaTime,currentSpeed);
}
然后根据需要继续捕捉:
Then proceeds to snap if needed:
-(void)snapIfNeeded
{
if(canStopScrolling && currentSpeed <70.0f && currentSpeed>-70.0f)
{
NSLog(@"Stopping with a speed of %f points per second", currentSpeed);
[self stopMoving];
float scrollDistancePastTabStart = fmodf(self.contentOffset.x, (self.frame.size.width/3));
float scrollSnapX = self.contentOffset.x - scrollDistancePastTabStart;
if(scrollDistancePastTabStart > self.frame.size.width/6)
{
scrollSnapX += self.frame.size.width/3;
}
float maxSnapX = self.contentSize.width-self.frame.size.width;
if(scrollSnapX>maxSnapX)
{
scrollSnapX = maxSnapX;
}
[UIView animateWithDuration:0.3
animations:^{self.contentOffset=CGPointMake(scrollSnapX, self.contentOffset.y);}
completion:^(BOOL finished){[self stopMoving];}
];
}
else
{
NSLog(@"Did not stop with a speed of %f points per second", currentSpeed);
}
}
-(void)stopMoving
{
if(self.dragging)
{
[self setContentOffset:CGPointMake(self.contentOffset.x, self.contentOffset.y) animated:NO];
}
canStopScrolling = NO;
}
以下是委托方法:
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
canStopScrolling = NO;
[self refreshCurrentSpeed];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
canStopScrolling = YES;
NSLog(@"Did end dragging");
[self snapIfNeeded];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self refreshCurrentSpeed];
[self snapIfNeeded];
}
除了两种情况外,大部分时间都有效:
1.当用户在没有释放他/她的手指的情况下滚动并且在移动后立即在接近静止的时间进行滚动时,它经常按照预期的方式捕捉到它的位置,但很多时候,它没有。通常需要几次尝试才能实现它。时间(非常低)和/或距离(相当高)的奇数值出现在释放时,导致高速值,而scrollView实际上几乎或完全静止。
2.当用户点击滚动视图以停止其移动时,似乎scrollview将 contentOffset
设置为其先前的位置。这种远距传送产生非常高的速度值。这可以通过检查先前的delta是否为currentDelta * -1来修复,但我更喜欢更稳定的解决方案。
This works well most of the time, except in two scenarios:1. When the user scrolls without releasing his/her finger and lets go at a near stationary timing right after moving, it often snaps to its position as it's supposed to, but a lot of times, does not. It usually takes a few attempts to get it to happen. Odd values for time (very low) and/or distance (rather high) appear at the release, causing a high speed value while the scrollView is, in reality, nearly or entirely stationary.2. When the user taps the scrollview to stop its movement, it seems the scrollview sets the contentOffset
to its previous spot. This teleportation results in a very high speed value. This could be fixed by checking if the previous delta is currentDelta*-1, but I'd prefer a more stable solution.
我尝试过使用 didEndDecelerating
,但是当出现毛刺时,它不会被调用。这可能证实它已经静止了。似乎没有在scrollview完全停止移动时调用的委托方法。
I've tried using didEndDecelerating
, but when the glitch occurs, it does not get called. This probably confirms that it's stationary already. There seems to be no delegate method that gets called when the scrollview stopped moving completely.
如果你想亲眼看到这个小故障,这里有一些代码来填充scrollview带标签:
If you'd like to see the glitch yourself, here's some code to fill the scrollview with tabs:
@interface UIScrollView <UIScrollViewDelegate>
{
bool canStopScrolling;
float prevOffset;
float deltaOffset; //remembered for debug purposes
NSTimeInterval prevTime;
NSTimeInterval deltaTime; //remembered for debug purposes
float currentSpeed;
}
-(void)stopMoving;
-(void)snapIfNeeded;
-(void)refreshCurrentSpeed;
@end
@implementation TabScrollView
-(id) init
{
self = [super init];
if(self)
{
self.delegate = self;
self.frame = CGRectMake(0.0f,0.0f,320.0f,40.0f);
self.backgroundColor = [UIColor grayColor];
float tabWidth = self.frame.size.width/3;
self.contentSize = CGSizeMake(100*tabWidth, 40.0f);
for(int i=0; i<100;i++)
{
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(i*tabWidth,0.0f,tabWidth,40.0f);
view.backgroundColor = [UIColor colorWithWhite:(float)(i%2) alpha:1.0f];
[self addSubview:view];
}
}
return self;
}
@end
此问题的缩写版本:如何知道scrollview何时停止滚动? didEndDecelerating:
在你静止释放时没有被调用, didEndDragging:
在滚动和检查速度期间发生了很多由于这个奇怪的跳跃将速度设置为随机的,所以是不可靠的。
A shorter version of this question: how to know when the scrollview stopped scrolling? didEndDecelerating:
does not get called when you release it stationary, didEndDragging:
happens a lot during the scrolling and checking for speed is unreliable due to this odd 'jump' which sets the speed to something random.
推荐答案
我找到了一个解决方案:
I found a solution:
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
我没注意到之前的最后一位, willDecelerate
。当结束触摸时,scrollView静止时为false。结合上面提到的速度检查,我可以在慢速(不接触)或静止时捕捉它们。
I did not notice that last bit before, willDecelerate
. It is false when the scrollView is stationary when ending the touch. Combined with the above-mentioned speed check, I can snap both when it's slow (and not being touched) or when it's stationary.
对于没有做任何捕捉但需要的人要知道滚动停止时,将在滚动运动结束时调用 didEndDecelerating
。结合在 didEndDragging
中检查 willDecelerate
,您就会知道滚动何时停止。
For anyone not doing any snapping but needs to know when scrolling stopped, didEndDecelerating
will be called at the end of the scroll movement. Combined with a check on willDecelerate
in didEndDragging
, you'll know when the scrolling has stopped.
这篇关于如何准确知道UIScrollView的滚动何时停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!