Animation started in touchesBegan never ends

Viewed 34

I have a UITableViewCell that contains a cardView inside it. When I click on my cell I want that card view to run a affine animation. If I keep pressing the cell the affine effect should continue, if I releases the cell, the card should return back to normal (the identity transform is applied). But, sometimes, what is occurring is that I press my cell and the touchesBegan is called, but after, when I release the cell, neither the touchesEnded nor the touchesMoved is called, so my cell stays with the affine transformation applied.

My code for my cell is this one:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        UIView.animate(withDuration: 0.2,
                       delay: 0.1,
                       usingSpringWithDamping: 0.9,
                       initialSpringVelocity: 5,
                       options: [],
                       animations: {
                        
                        self.cardView.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
        })
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            UIView.animate(withDuration: 0.3, animations: {
                self.cardView.transform = .identity
            }, completion: { _ in
                self.didTouchesEnded.onNext(())
            })
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        UIView.animate(withDuration: 0.2,
                       delay: 0,
                       usingSpringWithDamping: 0.9,
                       initialSpringVelocity: 5,
                       options: .curveEaseIn,
                       animations: {
                        
            self.cardView.transform = .identity
        })
    }

enter image description here

0 Answers
Related