所以我试图实现这样的目标:
我可以向左和向右滑动的旋转木马,但我不知道如何实现这一点,我现在有一个集合视图,所有设置都是水平滚动。
还有其他办法吗?是否有POD或我可以使用的东西?它不需要使用我刚刚使用它测试的集合视图。
任何帮助将不胜感激
答案 0 :(得分:1)
在 Swift 3 。
extension UICollectionView {
var centerPoint : CGPoint {
get {
return CGPoint(x: self.center.x + self.contentOffset.x, y: self.center.y + self.contentOffset.y);
}
}
var centerCellIndexPath: IndexPath? {
if let centerIndexPath = self.indexPathForItem(at: self.centerPoint) {
return centerIndexPath
}
return nil
}
}
使用项目单元格制作Curosel
:
func curosel() {
if myCollectionView.centerCellIndexPath != nil {
var index = (myCollectionView.centerCellIndexPath?.row)!
if myCollectionView.centerCellIndexPath! == IndexPath(item: 0, section: 0) {
myCollectionView.cellForItem(at: myCollectionView.centerCellIndexPath!)?.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
index += 1
myCollectionView.cellForItem(at: IndexPath(item: index, section: 0))?.transform = CGAffineTransform.identity
} else {
myCollectionView.cellForItem(at: myCollectionView.centerCellIndexPath!)?.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
index -= 1
myCollectionView.cellForItem(at: IndexPath(item: index, section: 0))?.transform = CGAffineTransform.identity
index += 2
myCollectionView.cellForItem(at: IndexPath(item: index, section: 0))?.transform = CGAffineTransform.identity
}
myCollectionView.scrollToItem(at:IndexPath(item: index-1, section: 0), at: .centeredHorizontally, animated: true)
}
}
}
答案 1 :(得分:1)
我使用的一个很棒的吊舱叫做“ CollectionViewPagingLayout”,非常简单
import UIKit
import CollectionViewPagingLayout
class MyCustomCell: UICollectionViewCell, TransformableView {
func transform(progress: CGFloat) {
//customize it however you want
}
}
import UIKit
import CollectionViewPagingLayout
class MyCustomCell: UICollectionViewCell, ScaleTransformView {
var scaleOptions = ScaleTransformViewOptions(
minScale: 0.6,
scaleRatio: 0.4,
translationRatio: CGPoint(x: 0.66, y: 0.2),
maxTranslationRatio: CGPoint(x: 2, y: 0),
)
}
import UIKit
import CollectionViewPagingLayout
class viewController: UIViewController {
//Outlets
@IBOutlet var myCollectionView: UICollectionView!
//Variables
let layout = CollectionViewPagingLayout()
//View did load
override func viewDidLoad() {
super.viewDidLoad()
//collectionViewPagingLayout setup
layout.delegate = self
myCollectionView.collectionViewLayout = layout
myCollectionView.isPagingEnabled = true
}
}
extension ViewController: CollectionViewPagingLayoutDelegate {
func onCurrentPageChanged(layout: CollectionViewPagingLayout, currentPage: Int) {
print(currentPage)
}
}
layout.numberOfVisibleItems = 3
,您可以将其放入viewdidload()