我想做一个像旋转器一样滚动的圆形集合视图。
我试过的:
swift - 如何滚动循环CollectionView-LMLPHP
但CollectionView不会滚动。
链接到我使用的源代码:GitHub

最佳答案

您需要使用UIRotationGestureRecognizer
当用户以圆周运动相对移动手指时,基础视图应以相应的方向和速度旋转。
为此,您可以将识别器添加到UICollectionView

   let rotationGesture = UIRotationGestureRecognizer(target: self, action:     #selector(rotationRecognized(_:)))
   collectionView?.addGestureRecognizer(rotationGesture)

然后识别方向并手动添加或删除单元格:
   @objc func rotationRecognized(_ sender: UIRotationGestureRecognizer) {

        if sender.state == .began {
            print("begin")
        } else if sender.state == .changed {
            print("changing")
            let newRotation = sender.rotation
            print(newRotation)
        } else if sender.state == .ended {
            print("end")

            // Used 1 as an arbitrary minimum
            if(sender.rotation > 1) {
                self.collectionView?.performBatchUpdates({
                    self.numberOfCells += 1
                    self.collectionView?.insertItems(at: [IndexPath(item: 0, section: 0)])
                })
            }

            if(sender.rotation < 1) {
                self.collectionView?.performBatchUpdates({
                    self.numberOfCells -= 1
                    self.collectionView?.deleteItems(at: [IndexPath(item: 0, section: 0)])
                })
            }

        }
    }

输出:
swift - 如何滚动循环CollectionView-LMLPHP
编辑
要简单地旋转一个视图,您需要保存它以前的旋转并将其与CGAffineTransform(rotationAngle:)相加。由于示例的collectionview是全屏的,所以我需要确定大小并将其居中于UIVIewController
swift - 如何滚动循环CollectionView-LMLPHP
那么,代码应该是这样的:
import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    var numberOfCells = 10
    var lastRotation: CGFloat = 0
    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.collectionViewLayout = CircleLayout()
        collectionView.delegate = self
        collectionView.dataSource = self
        let rotationGesture = UIRotationGestureRecognizer(target: self, action:     #selector(rotationRecognized(_:)))
        collectionView.addGestureRecognizer(rotationGesture)

    }

    @objc func rotationRecognized(_ sender: UIRotationGestureRecognizer) {

        if sender.state == .began {
            print("begin")
            sender.rotation = lastRotation
        } else if sender.state == .changed {
            print("changing")
            let newRotation = sender.rotation + lastRotation
            collectionView.transform = CGAffineTransform(rotationAngle: newRotation)
        } else if sender.state == .ended {
            print("end")
            lastRotation = sender.rotation
        }
    }

    // update collection view if size changes (e.g. rotate device)

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        coordinator.animateAlongsideTransition(in: view, animation: { _ in
            self.collectionView?.performBatchUpdates(nil)
        })
    }
}

// MARK: UICollectionViewDataSource

extension ViewController {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return numberOfCells
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CircleCell", for: indexPath)
        return cell
    }
}


这是输出:
swift - 如何滚动循环CollectionView-LMLPHP

关于swift - 如何滚动循环CollectionView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57745522/

10-14 16:51