Search code examples
swiftuicollectionviewuicollectionviewcelluigesturerecognizeruitapgesturerecognizer

tap gesture not responding while animating height


I'm playing with tap gesture to animate height of UIView what I want to achieve is open close or do some drop down. my tap gesture work if I'm not using if else statement. but not work the other way around. this is my code.

class DailyAbsenceCell: UICollectionViewCell {

    @IBOutlet weak var mainContainer: UIView!
    @IBOutlet weak var calendarView: JKCalendar!
    @IBOutlet weak var mainContainerHeightConstraint: NSLayoutConstraint!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        mainContainer.addGestureRecognizer(tapGesture)
        mainContainerHeightConstraint.constant = 75
        calendarView.alpha = 0
    }

    @objc func handleTap(gesture: UITapGestureRecognizer) {
        if gesture.state == .changed {
            mainContainerHeightConstraint.constant = 370
            calendarView.alpha = 1
            print("Tapped")
        } else {
            mainContainerHeightConstraint.constant = 75
            calendarView.alpha = 0
        }
    }
}

Thank you!!


Solution

  • if gesture.state == .changed will never be true. A tap gesture recognizer does not have a changed state. In fact there is no need to check its state at all.

    Change this to

    if mainContainerHeightConstraint.constant == 75