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

问题描述 投票:2回答:1

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

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)
        }
    }
}
ios swift uiswipegesturerecognizer
1个回答
2
投票

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

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

为UICollectionView设置委托和dataSource

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

根据您的json数组计数返回单元格数

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
}

有关如何使用自定义UICollectionViewCell的大量SO问题。只需随机搜索其中一个:qazxsw poi

© www.soinside.com 2019 - 2024. All rights reserved.