Как вызвать функцию UICollectionViewCell внутри UICollectionViewController? - PullRequest
0 голосов
/ 03 ноября 2019

Я хотел бы вызвать функцию animation3 () (которая является функцией внутри PageCell (), которая является UICollectionViewCell) внутри BreathingSwpipingController, который является UICollectionViewController.

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()

}

}

1 Ответ

1 голос
/ 03 ноября 2019

Сделайте 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()
}
...