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

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

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

問題描述

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

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).它的要求之一是它在低于一定速度時停止?jié)L動,以允許用戶更快地交互.它還應(yīng)該捕捉到選項卡的開頭.換句話說,當(dāng)用戶釋放滾動視圖并且它的速度很低時,它會捕捉到一個位置.我已經(jīng)實現(xiàn)了這個并且它可以工作,但是它有一個錯誤.

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.

我現(xiàn)在擁有的:

滾動視圖是它自己的委托.在每次調(diào)用 scrollViewDidScroll: 時,它都會刷新與速度相關(guān)的變量:

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);
}

然后根據(jù)需要繼續(xù)捕捉:

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];
}

這在大多數(shù)情況下都很有效,除了以下兩種情況:1.當(dāng)用戶在不松開手指的情況下滾動并在移動后立即在接近靜止的時間松開時,它通常會按預(yù)期的位置捕捉??到它應(yīng)該的位置,但很多時候不會.通常需要幾次嘗試才能實現(xiàn).時間(非常低)和/或距離(相當(dāng)高)的奇數(shù)值出現(xiàn)在發(fā)布時,導(dǎo)致高速值,而實際上滾動視圖幾乎或完全靜止.2. 當(dāng)用戶點擊滾動視圖停止其移動時,滾動視圖似乎將 contentOffset 設(shè)置為之前的位置.這種瞬移導(dǎo)致非常高的速度值.這可以通過檢查之前的增量是否為 currentDelta*-1 來解決,但我更喜歡更穩(wěn)定的解決方案.

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,但是當(dāng)故障發(fā)生時,它不會被調(diào)用.這可能證實它已經(jīng)是靜止的.當(dāng)滾動視圖完全停止移動時,似乎沒有調(diào)用委托方法.

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

這個問題的簡短版本:如何知道滾動視圖何時停止?jié)L動?didEndDecelerating: 當(dāng)你靜止釋放它時不會被調(diào)用,didEndDragging: 在滾動過程中發(fā)生了很多,并且檢查速度是不可靠的,因為這種奇怪的跳躍"會設(shè)置速度到隨機的東西.

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.結(jié)束觸摸時滾動視圖靜止時為假.結(jié)合上面提到的速度檢查,我可以在它很慢(并且沒有被觸摸)或靜止時捕捉它.

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.

對于沒有進行任何捕捉但需要知道何時停止?jié)L動的任何人,didEndDecelerating 將在滾動運動結(jié)束時調(diào)用.結(jié)合對 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.

這篇關(guān)于如何準確知道 UIScrollView 的滾動何時停止?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

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(以編程方式設(shè)置 contentOffset 觸發(fā) scrollViewDidScroll)
Photos app-like gap between pages in UIScrollView with pagingEnabled(使用 pagingEnabled 的 UIScrollView 中頁面之間的照片應(yīng)用程序式間隙)
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)
主站蜘蛛池模板: 日韩免费在线视频 | 亚洲区视频 | 色噜噜狠狠一区二区三区 | 欧美精品亚洲 | 成人涩涩| 日韩欧美视频在线 | 在线观看av不卡 | 丁香午夜| 午夜免费影院 | 亚洲欧美日韩一区 | 欧美日韩亚洲国产 | 好好的日com | 这里只有精品在线观看 | 亚洲精品福利视频 | 国产免费自拍视频 | 欧美福利在线 | 婷婷激情综合 | 在线不卡av | 国产一区免费视频 | 亚洲精品一区二区三 | 一区二区三区久久久 | 日本免费中文字幕 | a视频| 国产成人在线免费观看 | 欧美黄色网| 精品国产精品三级精品av网址 | 成人区精品一区二区婷婷 | 一区二区在线看 | 日韩精品一级 | 天天色天天| 免费黄色一级视频 | 亚洲综合伊人 | 久久精品av | 午夜在线国语中文字幕视频 | 久久久久久久久久久国产 | 狠狠涩| 国产理论在线观看 | 天天拍天天干 | 亚洲免费观看视频 | 91视频播放| 国产视频一|