How to speed up scrolling in UIScrollView? [closed]

风流意气都作罢 提交于 2019-12-03 16:09:55

Tweak the decelerationRate of your UIScrollView.

More reading: UIScrollView Class Reference

If I understand your question correctly, you've setup the ScrollView to snap to pages (pagingEnabled = YES). When the the user lifts their finger you want it to snap to the closest page quicker than what it currently does?

If that's what you're trying to accomplish, this is what I recommend:

  • DISABLE pagingEnabled (pagingEnabled = NO). We're going to do it ourselves.
  • Setup some object (probably the ViewController) as a delegate of the scroll view.
  • In the Scrollview delegate, override the method scrollViewDidEndDragging:willDecelerate:. You will also want to override the method scrollViewWillEndDragging:withVelocity:targetContentOffset: so that you can get the velocity of the drag.
  • In the scrollViewDidEndDragging:willDecelerate:, you can calculate the direction of the drag (using the velocity you got from the other method) and the offset to the nearest page. From there, it's as simple as setting the contentOffset of the scrollview using a UIView animation block with the desired duration. For example:

[UIView animateWithDuration:.25f animations:^{ scrollview.contentOffset = calculatedCGPoint; }];

That should give you the desired effect.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!