如何在同一方向上滑动两个以上的视图?在滑动到其他视图时是否可以传递相同数量的数据?

时间:2017-12-20 04:12:24

标签: ios swift uiswipegesturerecognizer

我已经编写了以下代码,我从不同的博客和网站引用,并没有错误..但我没有得到适当的输出。还尝试在下面的代码中添加一些其他代码。不知道它有什么问题。

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

1 个答案:

答案 0 :(得分:2)

注册UICollectionViewCell(假设您没有使用任何自定义单元格)

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

为UICollectionView设置委托和dataSource

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

根据您的jsonArray计数返回单元格数

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

从jsonArray

填充您的UICollectionView
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
}

有关如何使用自定义UICollectioView Cell的大量SO问题。 只需随机搜索其中一个: how to use custom cell in collection view controller in swift 3