[英]Swift Carousel with collection views POD
So I'm trying to achieve something like this:所以我试图实现这样的目标:
A carousel where I can swipe left and right, but I'm not sure how can I achieve this, I have a collection view all setup with horizontal scroll right now.我可以左右滑动的旋转木马,但我不确定如何实现这一点,我现在有一个带有水平滚动的集合视图设置。
Is there any other way to do it?有没有其他方法可以做到? Is there a POD or something that I can use?
是否有 POD 或我可以使用的东西? it doesn't need to use collection views I just tested using this.
它不需要使用我刚刚使用它测试过的集合视图。
Any help will be appreciated任何帮助将不胜感激
A great pod that I've used is called "CollectionViewPagingLayout" it's very simple我用过的一个很棒的 pod 叫做“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
you would put this within viewdidload()layout.numberOfVisibleItems = 3
你会把它放在 viewdidload() 中In Swift 3 .在斯威夫特 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
}
}
To make Curosel
with Item cells:要使用项目单元格制作
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)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.