如何在iOS 10 Swift 3中创建水平轮播 [英] How to create horizontal carousel in iOS 10 Swift 3

查看:235
本文介绍了如何在iOS 10 Swift 3中创建水平轮播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我已从Android世界切换到iOS.我已经看到了两个用于创建水平滑块的选项.我的目标是创建此图像上存在的卡片的水平滑块. 我对iOS中最好的选项需要哪个元素感到困惑.我应该为此使用CollectionView还是为此使用PageViewController? 由于我是新手,因此在我所掌握的知识有限的情况下,无法在此阶段使用库.请建议我该怎么做

Recently I have switched to iOS from Android world. I have seen two options for creating horizontal sliders. My goal is to create an horizontal slider of cards present on this image. I got confused which element need to the best option in iOS. Should I use CollectionView for this OR should I use PageViewController for this? Since I am beginner I cannot use library at this stage with limited knowledge I have. Please suggest me how should I do it

推荐答案

简短答案

您应该使用UICollectionView.

Short answer

You should use a UICollectionView.

1)使用UICollectionViewFlowLayout

let layout = UICollectionViewFlowLayout()
// The size of each item. Pick a suitable height so that the items do not get stacked:
layout.itemSize = CGSize(width: view.frame.width / 2, height: view.frame.height)
// The most important part:
layout.scrollDirection = .horizontal
// Then initialize collectionView
let collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)

2)设置缩放比例(伪代码)

2) Setup scaling (pseudocode)

 一世.让self实现UIScrollViewDelegate方法scrollViewDidScroll(UIScrollView)

  i. Have self implement UIScrollViewDelegate method scrollViewDidScroll(UIScrollView)

  ii.设置collectionView.delgate = self

  ii. Set collectionView.delgate = self

  iii.在scrollViewDidScroll(UIScrollView)中,通过强制转换scrollView as! UICollectionView并获取visibleCells属性来获取所有可见的单元格.

  iii. In scrollViewDidScroll(UIScrollView), get all visible cells by casting scrollView as! UICollectionView and getting visibleCells property.

  iv.对于可见单元格中的单元格,请使用给定的单元格距屏幕水平中心的距离CGAffineTransform设置适当的比例(单元格内容,而不是单元格本身).这可能需要一些算术运算.这应该可以正常工作:

  iv. For cell in visible cells, set appropriate scale (of the cell contents, not of the cell itself) with CGAffineTransform given cell's distance from the horizontal center of the screen. This may take some arithmetic; this should work just fine:

s = −0.00005 * pow(cell.center.x, 2) + 1.5


希望这会有所帮助!如有需要,请随时澄清.


Hope this helps! Feel free to ask for clarification if you need it.

这篇关于如何在iOS 10 Swift 3中创建水平轮播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆