Search code examples
iosswiftxcodeuicollectionviewcardlayout

Cardlayout swipe left programmatically not working


I am using pod 'CardsLayout' to my project.

cardlayout library

The image is

original image

this library doesn't have functionality of swipe left and swipe right programmatically. so, i checked that, it is using UICollectionView. So, i thought that i can use selectItem by following code.

 func onNextButtonClicked(collectionViewCell:QuestionrieViewCell) {
  
    let currentIndex = collectionViewCell.getIndexPath().row
    if currentIndex < getIndexPaths().count-1{
        collectionView.deselectItem(at: collectionViewCell.getIndexPath(), animated: true)
        collectionView.selectItem(at: getIndexPaths()[collectionViewCell.getIndexPath().row + 1], animated: true, scrollPosition: .left)
        self.collectionView(collectionView, didSelectItemAt:getIndexPaths()[collectionViewCell.getIndexPath().row + 1] )
    }
}

func onPrevButtonClicked(collectionViewCell:QuestionrieViewCell) {
    let currentIndex = collectionViewCell.getIndexPath().row
    if currentIndex > 0{
       collectionView.deselectItem(at: collectionViewCell.getIndexPath(), animated: true)
       collectionView.selectItem(at: getIndexPaths()[collectionViewCell.getIndexPath().row - 1], animated: true, scrollPosition: .right)
       self.collectionView(collectionView, didSelectItemAt:getIndexPaths()[collectionViewCell.getIndexPath().row - 1] )
    }
}

I want that, swipe left and swipe right should be happen when the user click the buttons. here,

onPreviousButtonClicked working fine. whereas, swipe right button click not responding like i expected.

onNextBUttonClick gives weird response. response as below

nextbuttonclick response

it's stopped going to left and if i click the same right arrow button, on that stopped card, then it still slide little bit of the screen then stopped going to left.i don't know what should i do. Anyone can help me?

Edit

Additional images below.

second slide image

third slide image

fourth slide image

fifth slide image

ViewController Class is here

class ControllerQuestionarie: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate,QuestionrieViewCellDelegate{

var currentIndexPath:IndexPath?
  var indexPaths:[IndexPath] = [IndexPath]()
var swipeToLeft:UISwipeGestureRecognizer?,swipeToRight:UISwipeGestureRecognizer?
var previousPage : Int = 0

@IBOutlet weak var linearProgressBar: LinearProgressBar!
@IBOutlet weak var lblQuestionNumber: UILabel!



func setIndexPaths(indexPath:IndexPath){
      self.indexPaths.append(indexPath)
  }
  func getIndexPaths()->[IndexPath]{
      return self.indexPaths
  }
  
func onNextButtonClicked(collectionViewCell:QuestionrieViewCell) {
  
    let currentIndex = collectionViewCell.getIndexPath().row
    if currentIndex < getIndexPaths().count-1{
        self.collectionView.selectItem(at: IndexPath(item: collectionViewCell.getIndexPath().row + 1, section: 0), animated: true, scrollPosition: .left)
        self.collectionView(self.collectionView, didSelectItemAt: IndexPath(item: collectionViewCell.getIndexPath().row + 1, section: 0))
        self.collectionView.layoutIfNeeded()
        self.currentIndexPath = collectionViewCell.getIndexPath()
        if self.currentIndexPath!.row == 4{
                       let controllerQuestionarieCompletionViewController = ControllerQuestionarieCompletionViewController.init(nibName: "ControllerQuestionarieCompletionViewController", bundle: nil)
                                 self.navigationController!.pushViewController(controllerQuestionarieCompletionViewController, animated: true)
                   }
        
    }
}

 func onPrevButtonClicked(collectionViewCell:QuestionrieViewCell) {
     let currentIndex = collectionViewCell.getIndexPath().row
     if currentIndex > 0{
        self.collectionView.selectItem(at: IndexPath(item: collectionViewCell.getIndexPath().row - 1, section: 0), animated: true, scrollPosition: .right)
        self.collectionView(self.collectionView, didSelectItemAt: IndexPath(item: collectionViewCell.getIndexPath().row - 1, section: 0))
        self.collectionView.layoutIfNeeded()
        self.currentIndexPath = collectionViewCell.getIndexPath()
    }
}


 override func viewDidLoad() {
      super.viewDidLoad()
      
      collectionView.register(UINib.init(nibName: "QuestionrieViewCell", bundle: nil), forCellWithReuseIdentifier: "QuestionrieViewCell")
      collectionView.collectionViewLayout = CardsCollectionViewLayout()

      
      collectionView.dataSource = self
      collectionView.delegate = self
      collectionView.isPagingEnabled = true
    
  
      collectionView.showsHorizontalScrollIndicator = false
    
         
      let progressValue = 100/CGFloat(colors.count)
      let progress = CGFloat(progressValue) * CGFloat(previousPage + 1)
      lblQuestionNumber.text = "\(previousPage + 1) / \(colors.count)"
      linearProgressBar.progressValue = progress
             
    
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    self.currentIndexPath = indexPath
    // ... Other settings
   
         
  
    
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let pageWidth = scrollView.frame.size.width
        let page = Int(floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1)
      
        print (page)
        if previousPage != page{
            let progressValue = 100/CGFloat(colors.count)
            let progress = CGFloat(progressValue) * CGFloat(page + 1)
            lblQuestionNumber.text = "\(page + 1) / \(colors.count)"
            linearProgressBar.progressValue = progress
        }
        previousPage = page
}




func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "QuestionrieViewCell", for: indexPath) as! QuestionrieViewCell
    cell.questionrieViewCellDelegate = self
    cell.layer.cornerRadius = 50.0
    cell.setIndexPath(indexPath: indexPath)
    setIndexPaths(indexPath: indexPath)
    cell.bindData()

    cell.view.backgroundColor = colors[indexPath.row]
    
    return cell
}



@IBOutlet var collectionView: UICollectionView!



  var colors: [UIColor]  = [
    UIColor(red: 255, green: 255, blue: 255),
    UIColor(red: 249, green: 220, blue: 92),
    UIColor(red: 194, green: 234, blue: 189),
    UIColor(red: 1, green: 25, blue: 54),
    UIColor(red: 255, green: 184, blue: 209)
  ]



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




 extension UIColor {
   convenience init(red: Int, green: Int, blue: Int) {
     self.init(red: CGFloat(red)/255 ,
             green: CGFloat(green)/255,
             blue: CGFloat(blue)/255,
             alpha: 1.0)
     }
 }

Solution

  • Yes. I did it. Finally i have tasted the answer for this question.

     public func moveToPage(position:Int){
        let totalPage = collectionView.contentOffset.x/collectionView.frame.width// getting totalNumberOfPage
        if position < Int(totalPage){
            scrollToPage(page: position, animated: true)
        }
    }
    public  func moveToNextPage()->Int{
            let pageIndex = round(collectionView.contentOffset.x/collectionView.frame.width)
            let pageNo = Int(pageIndex+1)
            scrollToPage(page: pageNo, animated: true)
            return pageNo
    }
    public  func moveToPreviousPage()->Int{
            let pageIndex = round(collectionView.contentOffset.x/collectionView.frame.width)
            let pageNo = Int(pageIndex-1)
            scrollToPage(page: pageNo, animated: true)
            return pageNo
    }
           
        
    func scrollToPage(page: Int, animated: Bool) {
        var frame: CGRect = self.collectionView.frame
        frame.origin.x = frame.size.width * CGFloat(page)
        frame.origin.y = 0
        self.collectionView.scrollRectToVisible(frame, animated: animated)
    }
    

    It works like charm. Thank you