Кнопка в представлении коллекции (UPCarouselFlowLayout) не получает событие onclick - PullRequest
0 голосов
/ 22 мая 2019

Я создал вид карусели, используя UPCarouselFlowLayout, и он работает хорошо.Но когда нажата UIButton в представлении, никаких действий не происходит.Как сделать так, чтобы при нажатии UIButton вызывалось определенное действие?

CollectionViewCell :

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 вызывается определенная функция.

Ответы [ 2 ]

1 голос
/ 22 мая 2019

Попробуйте этот подход!Также Вы можете позвонить на закрытие на 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
     }
 }
0 голосов
/ 22 мая 2019

Вам не хватает IBAction вашей кнопки в MagicCollectionViewCell.Поэтому добавьте действие для вашей кнопки:

    @IBAction func buttonAction(_ sender: UIButton) {

}
...