Saurabh
Saurabh

Reputation: 745

how can I swipe more than two views in the same direction? Is it possible to pass the same amount of data while swiping to other views?

I have written the following code which i have referred from different blogs and sites and there are no errors..still i am not getting appropriate output. Also tried adding some other codes in the code below. Not sure what's wrong with it.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var view3: UIView!
    @IBOutlet weak var view2: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.handleSwipe1(sender:)))
        let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.handleSwipe1(sender:)))

       leftSwipe.direction = .left
       rightSwipe.direction = .right

       view.addGestureRecognizer(leftSwipe1)
       view.addGestureRecognizer(rightSwipe1)

    }

    @objc func handleSwipe1(sender: UISwipeGestureRecognizer){
        if(sender.direction == .left){
            let position = CGPoint(x: self.view3.frame.origin.x - 400.0, y: self.view3.frame.origin.y)
            view3.frame = CGRect(x: position.x, y: position.y, width: self.view3.frame.size.width, height: self.view3.frame.size.height)
        }

        if(sender.direction == .left){
            let position = CGPoint(x: self.view2.frame.origin.x - 400.0, y: self.view3.frame.origin.y)
            view3.frame = CGRect(x: position.x, y: position.y, width: self.view3.frame.size.width, height: self.view3.frame.size.height)
        }

        if(sender.direction == .right){
            let position = CGPoint(x: self.view3.frame.origin.x + 400.0, y: self.view3.frame.origin.y)
            view3.frame = CGRect(x: position.x, y: position.y, width: self.view3.frame.size.width, height: self.view3.frame.size.height)
        }
    }
}

Upvotes: 2

Views: 123

Answers (1)

koropok
koropok

Reputation: 1413

Registering UICollectionViewCell (assuming you are not using any custom cells)

self.collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "mycell")

Set your delegate and dataSource for UICollectionView

self.collectionView.delegate = self
self.collectionView.dataSource = self

Return the number of cells base on your jsonArray count

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.jsonArray.count
}

Populate your UICollectionView from your jsonArray

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mycell", for: indexPath)

     // Give what ever data to your cell
     // cell.titleLabel.text = self.jsonArray[indexPath.row] // Use indexPath.Row for the correct index of the jsonArray
     return cell
}

There are tons of SO questions on how to use a custom UICollectioView Cell. Just a random search for one of them: how to use custom cell in collection view controller in swift 3

Upvotes: 2

Related Questions