Predicting the resting offset in a UIScrollView after deceleration

前端 未结 3 1158
臣服心动
臣服心动 2020-12-25 12:40

I would like to be able to predict the final resting offset within a UIScrollView after a flick gesture. It doesn\'t need to be pixel-accurate, but close enough so that the

相关标签:
3条回答
  • 2020-12-25 13:03

    You could watch the speed of scrolling while the UIScrollView is decelerating, and when the speed falls beneath a certain value, snap to the nearest menu item.

    You will likely want to implement the UIScrollViewDelegate methods scrollViewDidEndDragging:willDecelerate: and scrollViewDidScroll:. When the user lifts their finger to finish a scroll gesture, scrollViewDidEndDragging:willDecelerate: will be called. Begin monitoring the scrollViewDidScroll: events, and watch the delta between the UIScrollView's current position and its previous position, divided by the time delta:

    float velocity = (currentPosition - lastPosition) / (currentTime - lastTime);
    

    When the velocity falls below a minimum value, call scrollRectToVisible:animated: on the UIScrollView.

    0 讨论(0)
  • 2020-12-25 13:10

    I cannot comment (I guess I need 50 rep), so this is not a full answer, but given the 0.998 and 0.990 values for deceleration, my instincts are that they simply multiply the current velocity by the deceleration rate each frame.

    0 讨论(0)
  • 2020-12-25 13:12

    Control UIScrollView's targetContentOffset in iOS 5

    iOS5 UIScrollViewDelegate has a new method: scrollViewWillEndDragging:withVelocity:targetContentOffset:.

    This fits perfectly with what you want to do.

    This method is not called when the value of the scroll view’s pagingEnabled property is YES. Your application can change the value of the targetContentOffset parameter to adjust where the scrollview finishes its scrolling animation.

    Alternative Solution for iOS4 and Lower

    Change the UIScrollView's decelerationRate to UIScrollViewDecelerationFast and then in scrollViewDidEndDecelerating move to the closest "page".

    The fast deceleration makes the complete stopping / sliding over a little more natural / less obnoxious.

    0 讨论(0)
提交回复
热议问题