Swift 如何滚动圆形集合视图
我想制作一个循环集合视图,它可以像旋转器一样滚动 我尝试的是: 但是collectionView不会滚动 链接到我使用的源代码:您需要使用: 当用户以圆周运动相对移动手指时,基础视图应以相应的方向和速度旋转 为此,您可以将识别器添加到Swift 如何滚动圆形集合视图,swift,uicollectionview,carousel,Swift,Uicollectionview,Carousel,我想制作一个循环集合视图,它可以像旋转器一样滚动 我尝试的是: 但是collectionView不会滚动 链接到我使用的源代码:您需要使用: 当用户以圆周运动相对移动手指时,基础视图应以相应的方向和速度旋转 为此,您可以将识别器添加到UICollectionView: let rotationsignature=ui旋转手势识别器(目标:自我,动作:#选择器(旋转识别(:)) collectionView?.AddGestureRecognitor(旋转手势) 然后识别方向并手动添加或删除单
UICollectionView
:
let rotationsignature=ui旋转手势识别器(目标:自我,动作:#选择器(旋转识别(:))
collectionView?.AddGestureRecognitor(旋转手势)
然后识别方向并手动添加或删除单元格:
@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)])
})
}
}
}
这是输出:
使用平移手势和pingesture@MojtabaHosseini你能提供一些代码吗?你能不能旋转而不移除和插入,我想像旋转器一样旋转我提到的库只用于集合视图的外观…效果不好,因为如果你用图像更改蓝点。每个图像都将旋转。我只想像旋转木马一样旋转轮子
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
}
}