53
49

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UITableViewでスクロールをAppスイッチャー風に止める

53
Last updated at Posted at 2015-02-23

UITableViewのスクロールをiOSのApp切り替え画面みたいな感じで、慣性に違和感を感じさせずに好きな位置で止めるやり方。

MyTableViewController.swift
class MyTableViewController: UITableViewController {
	override func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        let y = //targetConttentOffset.memory.y とか scrollView.frame.height とかをゴニョゴニョしてお好みの位置に
        targetContentOffset.memory.y = y
    }
}

これだけ。targetContentOffsetが指している値が、このまま行くと止まるはずの場所。
この値を書き換えてあげれば、慣性に違和感を感じさせずにスクロールしてくれる。

scrollViewWillEndDraggingは、ユーザが指を離したタイミングで必ず呼ばれるっぽい。
なので、scrollViewDidEndDeceleratingとか、scrollViewDidEndDragging:willDecelerateとかでゴニョゴニョする必要はない。

[参考にしたstackoverflow](http://stackoverflow.com/questions/9367600/custom-uiscrollview-paging-with-scrollviewwillenddragging stackoverflow)
リファレンス

53
49
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
53
49

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?