久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何準確知道 UIScrollView 的滾動何時停止?

How to know exactly when a UIScrollView#39;s scrolling has stopped?(如何準確知道 UIScrollView 的滾動何時停止?)
本文介紹了如何準確知道 UIScrollView 的滾動何時停止?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

簡而言之,我需要確切地知道滾動視圖何時停止滾動.我所說的停止滾動"是指它不再移動且不再被觸摸的那一刻.

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.

我現在擁有的:

滾動視圖是它自己的委托.在每次調用 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.當用戶在不松開手指的情況下滾動并在移動后立即在接近靜止的時間松開時,它通常會按預期的位置捕捉??到它應該的位置,但很多時候不會.通常需要幾次嘗試才能實現.時間(非常低)和/或距離(相當高)的奇數值出現在發布時,導致高速值,而實際上滾動視圖幾乎或完全靜止.2. 當用戶點擊滾動視圖停止其移動時,滾動視圖似乎將 contentOffset 設置為之前的位置.這種瞬移導致非常高的速度值.這可以通過檢查之前的增量是否為 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,但是當故障發生時,它不會被調用.這可能證實它已經是靜止的.當滾動視圖完全停止移動時,似乎沒有調用委托方法.

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.

如果您想親自查看故障,這里有一些代碼可以用標簽填充滾動視圖:

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

這個問題的簡短版本:如何知道滾動視圖何時停止滾動?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.

推薦答案

我找到了解決方案:

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

我之前沒有注意到最后一點,willDecelerate.結束觸摸時滾動視圖靜止時為假.結合上面提到的速度檢查,我可以在它很慢(并且沒有被觸摸)或靜止時捕捉它.

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 的滾動何時停止?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to subclass UIScrollView and make the delegate property private(如何繼承 UIScrollView 并使委托屬性私有)
Swift - how to get last taken 3 photos from photo library?(Swift - 如何從照片庫中獲取最后拍攝的 3 張照片?)
Setting contentOffset programmatically triggers scrollViewDidScroll(以編程方式設置 contentOffset 觸發 scrollViewDidScroll)
Photos app-like gap between pages in UIScrollView with pagingEnabled(使用 pagingEnabled 的 UIScrollView 中頁面之間的照片應用程序式間隙)
why UIScrollView is leaving space from top in ios 6 and ios 7(為什么 UIScrollView 在 ios 6 和 ios 7 中從頂部留下空間)
UIScrollView pauses NSTimer while scrolling(UIScrollView 在滾動時暫停 NSTimer)
主站蜘蛛池模板: 毛片免费看的 | 精品日韩一区二区三区 | 91私密视频 | 国产精品一区久久久 | 91伦理片| 超碰成人免费观看 | 福利视频一区二区 | 夜夜草视频 | 成人欧美 | 午夜免费视频观看 | 色综合色综合 | 亚洲精品国产综合区久久久久久久 | 欧美日韩免费在线 | 欧美日韩一 | 精品久久久久久亚洲国产800 | 99热播放| 一区二区三区在线免费观看视频 | 日韩欧美二区 | 成人a免费 | 国产乱码久久久久久 | 国产一区日韩在线 | 亚洲导航深夜福利涩涩屋 | 先锋av资源网 | 中文字幕国产视频 | www成人免费 | www.色五月.com| 国产精品区一区二区三 | 亚州成人| 国产日韩一区二区三免费高清 | 免费av在线| 亚洲精品一区中文字幕乱码 | 亚洲免费网站 | 日韩在线不卡视频 | 亚洲欧美中文日韩在线v日本 | 91视频网址 | 999久久久精品 | 免费在线国产视频 | 久久夜视频 | 欧美精品欧美精品系列 | 嫩草视频入口 | 国产一区二区三区视频免费观看 |