Cardlayout 以编程方式向左滑动不起作用

Cardlayout swipe left programmatically not working

我在我的项目中使用 pod 'CardsLayout'。

cardlayout library

图片是

这个库没有以编程方式向左滑动和向右滑动的功能。所以,我检查了一下,它正在使用 UICollectionView。所以,我认为我可以通过以下代码使用 selectItem。

 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] )
    }
}

我想要这样,当用户单击按钮时应该向左滑动和向右滑动。这里,

onPreviousButtonClicked 工作正常。然而,向右滑动按钮点击没有像我预期的那样响应。

onNextBUttonClick 给出了奇怪的响应。回复如下

它停止向左移动,如果我在那个停止的卡片上单击同一个向右箭头按钮,它仍然会在屏幕上滑动一点点,然后停止向左移动 left.i 不知道我应该怎么做做。谁能帮帮我?

编辑

下面有更多图片。

ViewController Class 在这里

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)
     }
 }

是的。我做到了。终于尝到这个问题的答案了

 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)
}

它很有魅力。谢谢