如何在UICollectionViewController内部触发UICollectionViewCell的功能?

问题描述 投票:0回答:1

我想在作为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()

}

}

swift xcode uicollectionview uikit uicollectionviewcell
1个回答
0
投票

通过制定协议使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()
}
© www.soinside.com 2019 - 2024. All rights reserved.