[英]How to combine UISlider and CollectionView to autoresizing the cells of the UICollectionViewCell
I need to create the next UI我需要创建下一个 UI
But i don't know where to start and how does the UISlider update the CollectionView's cells.但我不知道从哪里开始以及 UISlider 如何更新 CollectionView 的单元格。
The idea is to use a ramdom JSON and render each item into the CollectionView's cells.这个想法是使用随机 JSON 并将每个项目渲染到 CollectionView 的单元格中。
// Set default value of how many cells to be displayed per row.
var numberOfItemsPerRow:Int = 4
override func viewDidLoad() {
super.viewDidLoad()
configureSlider()
}
func configureSlider() {
let slider = UISlider(frame:CGRect(x: 0, y: 0, width: 300, height: 20))
slider.center = self.view.center
// minimum number of rows required
slider.maximumValue = 1
// maximum number of rows required
slider.maximumValue = 10
slider.value = 1
slider.isContinuous = true
stepper.addTarget(self, action: #selector(sliderValueChanged(_:)), for: .valueChanged)
self.view.addSubview(stepper)
numberOfItemsPerRow = Int(stepper.value)
}
@objc func sliderValueChanged(_ sender:UISlider!) {
numberOfItemsPerRow = Int(sender.value)
self.collectionView.performBatchUpdates {
self.collectionView.reloadData()
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = (self.view.frame.width)/CGFloat(numberOfItemsPerRow)
return CGSize(width: width, height: width)
}
Hope you can configure remaining delegate methods.希望您可以配置剩余的委托方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.