最简单的方法是使用UICollectionView和UICollectionViewFlowLayout.
UICollectionViewFlowLayout开箱即用,非常强大.
基本上你需要:
UICollectionView到视图控制器.UICollectionView去与Flow选定的布局.UICollectionViewFlowLayout
上面的步骤应该足以具有垂直可滚动的卡片视图.但它看起来不会很棒!为了使它变得更好,您可能需要在用户结束滚动后完美地居中最合适的卡片.要实现粘性滚动,您需要使用以下UICollectionViewDelegate方法:
// compute collection view content offset in method bellow
override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
...
}
// compute targetContentOffset in method bellow
override func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
...
}
Run Code Online (Sandbox Code Playgroud)
...最后提示:在视图控制器的代码中使集合视图减速看起来很快:
self.collectionView?.decelerationRate = UIScrollViewDecelerationRateFast
Run Code Online (Sandbox Code Playgroud)
寻呼指标:
在您提供的示例中,有一个分页控件可以反映当前项目和整体项目计数.你需要UIPageControll手动添加; 你可以在故事板中做到这一点.更新页面控制状态的最佳位置是:
// method in UICollectionView delegate that you need to define.
// update your paging info in method bellow.
override func scrollViewDidScroll(scrollView: UIScrollView) {
...
}
Run Code Online (Sandbox Code Playgroud)
备注:
处理我列出的滚动视图的方法是由UICollectionViewDelegatefrom 继承的UIScrollViewDelegate.UICollectionView本身源于UIScrollViewDelegate.
| 归档时间: |
|
| 查看次数: |
2728 次 |
| 最近记录: |