所以我正在努力实现这样的目标:
一个轮播,我可以左右滑动,但我不确定如何实现这一点,我现在有一个带有水平滚动的集合视图所有设置。
还有其他方法吗?有 POD 或其他我可以使用的东西吗?它不需要使用我刚刚使用它测试过的集合视图。
任何帮助将不胜感激
我使用过的一个很棒的 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
你可以把它放在 viewdidload() 中在 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)
}
}
}
使用CardCarousel,你可以轻松实现这种效果。示例:
import CardCarousel
import SwiftUI
struct SwiftUIView: View {
@State var data = [...]
var body: some View {
CardCarouselView(data)
.cardLayoutSize(widthDimension: .fractionalWidth(0.7), heightDimension: .fractionalHeight(0.7))
.cardTransformMode(.liner(minimumScale: 0.9))
.cardCornerRadius(10)
.frame(height: 600)
}
}