ios - 集合视图 (UPCarouselFlowLayout) 中的按钮未收到 onclick 事件
问题描述
我使用创建了一个轮播视图UPCarouselFlowLayout
,并且效果很好。但是当UIButton
单击视图中的 时,不会发生任何操作。我如何做到这一点,以便在UIButton
单击时调用特定操作?
集合视图单元:
class MagicCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var magicview: UIView!
@IBOutlet weak var magicimage: UIImageView!
@IBOutlet weak var magiclabel: UILabel!
@IBOutlet weak var magicbutton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
self.magicview.isUserInteractionEnabled = true
// Initialization code
DispatchQueue.main.async {
self.magicview.layer.shadowColor = UIColor.gray.cgColor
self.magicview.layer.shadowOpacity = 0.5
self.magicview.layer.shadowOpacity = 10.0
self.magicview.layer.shadowOffset = .zero
self.magicview.layer.shadowPath = UIBezierPath(rect: self.magicview.bounds).cgPath
self.magicview.layer.shouldRasterize = false
}
}
}
Magic1.swift
import UPCarouselFlowLayout
class Magic1: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {
@IBOutlet weak var magiccollection: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
magiccollection.register(UINib.init(nibName: "MagicCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "magiccidentifier")
let floawLayout = UPCarouselFlowLayout()
floawLayout.itemSize = CGSize(width: UIScreen.main.bounds.width - 60.0, height: magiccollection.frame.size.height)
floawLayout.scrollDirection = .horizontal
floawLayout.sideItemScale = 0.8
floawLayout.sideItemAlpha = 1.0
floawLayout.spacingMode = .fixed(spacing: 5.0)
magiccollection.collectionViewLayout = floawLayout
magiccollection.delegate = self
magiccollection.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = sexeducollec.dequeueReusableCell(withReuseIdentifier: "magiccidentifier", for: indexPath) as!MagicCollectionViewCell
if indexPath.row == 0{
cell.magiclabel.text = "Tester title"
cell.sexedimage.image = #imageLiteral(resourceName: "merlin")
}else if indexPath.row == 1{
cell.magiclabel.text = "love is in the air"
}else{
cell.magiclabel.text = "Title - \(indexPath.row + 1)"
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == 0{
print("Accio")
}else if indexPath.row == 1{
print("alohamora")
}
}
}
我想要做的是当magicbutton
被点击indexPath.row == 0
它调用一个特定的动作,当它被点击indexPath.row == 1
它调用一个特定的函数。
解决方案
试试这个方法!此外,您可以在 IBAction 调用关闭
class ViewController: UICollectionViewController {
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "magiccidentifier", for: indexPath) as? MagicCollectionViewCell {
switch indexPath.row {
case 0:
cell.onCellTouched = { print("Hi")}
default:
cell.onCellTouched = { print("By") }
}
return cell
}
return UICollectionViewCell()
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let item = collectionView.cellForItem(at: indexPath) as? MagicCollectionViewCell
item?.onCellTouched()
}
}
class MagicCollectionViewCell: UICollectionViewCell {
var onCellTouched: () -> Void = { assertionFailure("Cell action didn't set up") }
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = .black
}
}
推荐阅读
- php - Wordpress 二十七主题 header.php 文件数据被自动删除
- java - Firebase - 在for循环中同步获取firebase数据?
- javascript - Konva - 缩放和旋转后更改偏移位置
- python - 如何编写输出源代码的程序?
- c - 使用 omp 线程的 2 种相等并行方式之间的实际差异
- python - 使用python逐文本比较两个不同的文件
- python - Python - 关于如何在 Python 中存储和处理字符串的困惑
- java - 单元测试:toString 方法。可能的覆盖?
- android - Android - 三星“主题” - 如何设计/使用我自己的主题(供个人使用)?
- mysql - 增加 SELECT 查询的列值