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

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

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

問題描述

簡而言之,我需要確切地知道滾動視圖何時停止?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動,以允許用戶更快地交互.它還應該捕捉到選項卡的開頭.換句話說,當用戶釋放滾動視圖并且它的速度很低時,它會捕捉到一個位置.我已經(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)在擁有的:

滾動視圖是它自己的委托.在每次調用 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);
}

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

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: 當你靜止釋放它時不會被調用,didEndDragging: 在滾動過程中發(fā)生了很多,并且檢查速度是不可靠的,因為這種奇怪的跳躍"會設置速度到隨機的東西.

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.

對于沒有進行任何捕捉但需要知道何時停止?jié)L動的任何人,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模板網(wǎng)!

【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(liá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(以編程方式設置 contentOffset 觸發(fā) 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)
主站蜘蛛池模板: 日本电影一区二区 | 久久久久久av | 在线天堂免费中文字幕视频 | 伊人狠狠操 | 精品国产一区二区三区久久久蜜月 | 国产福利精品一区 | 人成久久| 久久成人一区二区三区 | 国产一级片一区二区 | 日韩91在线| 精品久久久久一区二区国产 | av乱码 | 久久精品国产亚洲 | 免费黄视频网站 | 亚洲欧美日韩精品久久亚洲区 | 欧美色综合 | 国产一级电影在线 | 亚洲成人综合在线 | 欧美久久一级特黄毛片 | 一区二区电影网 | 日韩精品1区2区3区 国产精品国产成人国产三级 | 麻豆视频在线免费观看 | 一区二区三区欧美在线 | 久久精品国产一区二区三区不卡 | 天天操夜夜拍 | 亚洲一区二区在线视频 | 99久久久无码国产精品 | 精品久久免费 | 91精品国产综合久久久久久首页 | 成人激情免费视频 | 亚洲国产一区二区三区四区 | 福利视频网站 | 亚洲高清在线观看 | 四虎影音| 四虎影院在线观看免费视频 | 一区二区国产在线 | 日本免费视频在线观看 | 一区二区三区高清 | 青青草一区二区 | 亚洲 中文 欧美 日韩 在线观看 | 亚洲精品一 |