在UICollectionViewController内

在UICollectionViewController内

我想触发一个animation3()函数(这是PageCell()中的一个函数,它是一个UICollectionViewCell),它位于一个uiCollectionViewControllerBreakingsWPIPingcontroller中。

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 startstopteted(){
        pageCell!.animation3()

}
}

最佳答案

通过制定协议使PageCell成为BreathingSwipingController的代表

protocol BreathingSwipingDelegate: class {
 func doSth()
}

现在在UICollectionViewController内部定义aweak 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()
}

关于swift - 如何在UICollectionViewController内触发UICollectionViewCell的功能?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58681698/

10-10 20:33