Reputation: 10046
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? it doesn't need to use collection views I just tested using this.
Any help will be appreciated
Upvotes: 2
Views: 3212
Reputation: 1
Using CardCarousel, you can easily achieve this effect. CardCarousel is a carousel component that I wrote. Example:
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)
}
}
Upvotes: 0
Reputation: 216
A great pod that I've used is called "CollectionViewPagingLayout" it's very simple
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()Upvotes: 5
Reputation: 25
In 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
}
}
To make Curosel
with Item cells:
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)
}
}
}
Upvotes: 1