SDCenteredScrollView
示例
要求
此 pod 需要 iOS 9.0 或更高版本的目标平台
安装
SDCenteredScrollView 通过 CocoaPods 提供。要安装它,只需将以下行添加到 Podfile 中
pod 'SDCenteredScrollView'
程序化使用
导入 SDCenteredScrollView 库
import SDCenteredScrollView
创建并应用 SDCenteredCollectionViewLayout
private var collectionView: UICollectionView = {
let flowLayout = SDCenteredCollectionViewLayout()
flowLayout.scrollDirection = .horizontal
flowLayout.itemSize = CGSize(width: 100, height: 100)
flowLayout.minimumLineSpacing = 10//default = 10
flowLayout.standardItemAlpha = 0.5//default = 1
flowLayout.standardItemScale = 0.8//defaul = 1
let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
collectionView.backgroundColor = .clear
collectionView.showsHorizontalScrollIndicator = false
collectionView.showsVerticalScrollIndicator = false
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
collectionView.translatesAutoresizingMaskIntoConstraints = false
return collectionView
}()
调整焦点的中心单元格的内边距
extension ViewController: UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let leftEdgeInsets = collectionView.frame.size.width / 2 - ((self.cardSize.width) / 2)
let rightEdgeInsets = collectionView.frame.size.width / 2 - ((self.cardSize.width) / 2)
return UIEdgeInsets(top: 0, left: leftEdgeInsets, bottom: 0, right: rightEdgeInsets);
}
作者
杨伟良, [email protected]
许可版权
SDCenteredScrollView 在 MIT 许可协议下可用。有关更多信息,请参阅 LICENSE 文件。