zjfjack / JZCalendarWeekView

Calendar Week & Day View in iOS Swift
MIT License
448 stars 120 forks source link

Q: Unable to trigger button click which is inside LongPressWeekView cell. #67

Open ravinashreddy opened 5 years ago

ravinashreddy commented 5 years ago

Hi, This is really useful library. I am trying to customize the cell. So I created my own type of cell called CustomEventCell and registered in LongPressWeekView.

Inside this cell, I have label and an images( ex: favorite icon). I need to trigger some action when I click on this icon. But when I click the image/anywhere, collectionview's didSelect method is called.

How to trigger action/method/delegate when I click on image.

Here is my code for the cell

`class CustomEventCell: JZLongPressEventCell {

@IBOutlet weak var titleLabel: UITextView!

@IBOutlet weak var firstImageVIew: UIImageView!

@IBOutlet weak var secondImageView: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()

    setupBasic()
    // You have to set the background color in contentView instead of cell background color, because cell reuse problems in collectionview
    // When setting alpha to cell, the alpha will back to 1 when collectionview scrolled, which means that moving cell will not be translucent
    self.contentView.backgroundColor = UIColor.green.withAlphaComponent(0.7)
}

func setupBasic() {
    self.clipsToBounds = true

    layer.shadowColor = UIColor.black.cgColor
    layer.shadowOffset = CGSize(width: 0, height: 4)
    layer.shadowRadius = 5
    layer.shadowOpacity = 0

    let bgColor = UIColor.green

    self.titleLabel.setContentOffset(CGPoint.zero, animated: false)
    titleLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
    titleLabel.backgroundColor = bgColor.withAlphaComponent(0.7)
}

func configureCell(event: DefaultEvent) {
    self.event = event

    titleLabel.text = event.title

    let size: CGFloat =  30
    let imgSize = CGSize(width: size, height: size)

    firstImageVIew.image = UIImage.fontAwesomeIcon(name: .car, style: .solid, textColor: .red, size: imgSize)
    secondImageView.image = UIImage.fontAwesomeIcon(name: .car, style: .solid, textColor: .black, size: imgSize)

    let tgr = UITapGestureRecognizer(target: self, action: #selector(onFavClicked))
    secondImageView.isUserInteractionEnabled = true
   secondImageView.addGestureRecognizer(tgr)
}

@objc
func onFavClicked() {
 //This method is not called
    print("favorite icon clicked")
}

}

`

zjfjack commented 5 years ago

I tried your case with titleLabel, which works fine. Could you try it again? Is it might be your UIImageView problem?

ravinashreddy commented 5 years ago

I will try but I need many clicks in a row. I need row selection as well as button selections so that I trigger then separately. Will let u know. I will try with label once.

if it is the image problem like u said, how to solve it. I need the image in the place. Can't get rid of it.

ravinashreddy commented 5 years ago

I just tried

func setupBasic() {
        self.clipsToBounds = true

        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOffset = CGSize(width: 0, height: 4)
        layer.shadowRadius = 5
        layer.shadowOpacity = 0

        let bgColor = UIColor.green

        self.titleLabel.setContentOffset(CGPoint.zero, animated: false)
        titleLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        titleLabel.backgroundColor = bgColor.withAlphaComponent(0.7)

        let tgr = UITapGestureRecognizer(target: self, action: #selector(onFavClicked))
        titleLabel.isUserInteractionEnabled = true
        titleLabel.addGestureRecognizer(tgr)
    }

but it is still triggering didSeelct method. Could anyone help with this?

ravinashreddy commented 5 years ago

I am debugging view hierarchy and noticed that titleLabel is behind contentView image So I did

titleLabel.isUserInteractionEnabled = true titleLabel.addGestureRecognizer(tgr) contentView.bringSubviewToFront(titleLabel)

but it still does not click the label

Note: titleLabel is actually textview