我想在作为UICollectionViewController的BreathingSwpipingController内触发animation3()函数(该函数是PageCell()的一个UICollectionViewCell内的函数。
class BreathingSwipingController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var pageCell: PageCell?
覆盖func viewDidLoad(){
super.viewDidLoad()
collectionView.backgroundColor = .white
collectionView.register(PageCell.self, forCellWithReuseIdentifier: "cellId")
collectionView.isPagingEnabled = true
setupStartStopButton()
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
pageCell = (collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as? PageCell)!
return pageCell!
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
@@ objc func startStopTapped(){
pageCell!.animation3()
}
}
通过制定协议使PageCell
成为BreathingSwipingController
的代表
protocol BreathingSwipingDelegate: class {
func doSth()
}
现在UICollectionViewController
内部定义了weak var delegate: BreathingSwipingDelegate?
然后转到PageCell
文件并遵守该协议
class PageCell: BreathingSwipingDelegate .... {
func doSth() {
animation3()
}
var mController = BreathingSwipingController()
override init(style: .....) {
mController.delegate = self
.
.
.
.
}
}
现在在BreathingSwipingController
中,请执行以下操作
@objc func startStopTapped() {
delegate?.doSth()
}