class SwipeCardComponentVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
var dataSource = ["Card 1", "Card 2", "Card 3", "Card 4", "Card 5"] // Instance information supply
var removedCards: [(card: String, indexPath: IndexPath)] = [] // To retailer eliminated playing cards for undo
override func viewDidLoad() {
tremendous.viewDidLoad()
// Configure the customized structure
let structure = TinderLayout()
collectionView.collectionViewLayout = structure
collectionView.dataSource = self
collectionView.delegate = self
let panGesture = UIPanGestureRecognizer(goal: self, motion: #selector(handlePanGesture(_:)))
collectionView.addGestureRecognizer(panGesture)
}
// Assortment view information supply strategies
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
return dataSource.depend
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SwipeCardCell", for: indexPath) as! SwipeCardCell
let shade = ["DBA979","BACD92","9BB0C1","8E7AB5","80BCBD","C3E2C2","AFC8AD","D2E0FB"]
cell.bgView.backgroundColor = hexStringToUIColor(hex: String.getString(shade[indexPath.row]))
return cell
}
@objc func handlePanGesture(_ gesture: UIPanGestureRecognizer) {
let location = gesture.location(in: collectionView)
guard let indexPath = collectionView.indexPathForItem(at: location),
let cell = collectionView.cellForItem(at: indexPath) as? SwipeCardCell else {
return
}
let translation = gesture.translation(in: collectionView)
cell.heart.x += translation.x // Horizontal motion
gesture.setTranslation(.zero, in: collectionView)
if gesture.state == .ended {
let threshold: CGFloat = 100 // Outline threshold for swipe off
let course: CGFloat = translation.x > 0 ? 1 : -1
if abs(translation.x) > threshold {
if course < 0 { // Swipe left to take away
swipeLeftAction(cell, indexPath)
} else { // Swipe proper to append
swipeRightAction(cell, indexPath)
}
} else {
// Reset the cardboard's place if not swiped far sufficient
UIView.animate(withDuration: 0.3) {
cell.heart = self.collectionView.heart
}
}
}
}
func swipeLeftAction(_ cell: SwipeCardCell, _ indexPath: IndexPath) {
UIView.animate(withDuration: 0.3, animations: {
cell.heart.x -= 1000 // Swipe off to the left
}, completion: { _ in
// Retailer the eliminated card for potential undo
self.removedCards.append((self.dataSource[indexPath.item], indexPath))
// Take away the cardboard from the info supply and assortment view
self.dataSource.take away(at: indexPath.merchandise)
self.collectionView.deleteItems(at: [indexPath])
})
}
func swipeRightAction(_ cell: SwipeCardCell, _ indexPath: IndexPath) {
UIView.animate(withDuration: 0.3, animations: {
cell.heart.x = self.collectionView.heart.x // Reset the place
}, completion: nil)
}
}
//MARK:-----SetCollectionViewFlowLayout----
class TinderLayout: UICollectionViewLayout {
non-public let itemSize = CGSize(width: 400, top: 600) // Set the scale of every card
non-public let interItemSpacing: CGFloat = 10 // Area between stacked playing cards
override func put together() {
tremendous.put together()
}
override var collectionViewContentSize: CGSize {
guard let collectionView = collectionView else { return .zero }
return collectionView.bounds.dimension
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let collectionView = collectionView else { return nil }
var attributes: [UICollectionViewLayoutAttributes] = []
let numberOfItems = collectionView.numberOfItems(inSection: 0)
// Maintain the unique order to make sure the primary merchandise is on prime
for index in 0..<numberOfItems {
let indexPath = IndexPath(merchandise: index, part: 0)
if let attr = layoutAttributesForItem(at: indexPath) {
attributes.append(attr)
}
}
return attributes
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
guard let collectionView = collectionView else { return nil }
let attr = UICollectionViewLayoutAttributes(forCellWith: indexPath)
attr.dimension = itemSize
let heart = collectionView.heart
attr.heart = CGPoint(x: heart.x, y: heart.y)
// Alter place for overlapping impact
attr.remodel = CGAffineTransform(translationX: 0, y: -CGFloat(indexPath.merchandise) * interItemSpacing)
return attr
}
}
not working correctly please assist