pretz
pretz

Reputation: 242

UIButton Animations not working on UIScrollView

I have button animations that are not working on my ScrollView. I have other VCs that are just UIViews and animations work just fine. So I tried unchecking from the Attributes Inspector and adding...

self.scrollView.delaysContentTouches = false

...in my viewDidLoad, but it had effect.

I also found that tapping does not trigger the animation but holding on the button for a second or more does and haptic presses also trigger the animation.

This is my animation code:

extension UIButton {
    func startAnimatingPressActions() {
        addTarget(self, action: #selector(animateDown), for: [.touchDown, .touchDragEnter])
        addTarget(self, action: #selector(animateUp), for: [.touchDragExit, .touchCancel, .touchUpInside, .touchUpOutside])
    }

    @objc private func animateDown(sender: UIButton) {
        animate(sender, transform: CGAffineTransform.identity.scaledBy(x: 0.95, y: 0.95))
    }

    @objc private func animateUp(sender: UIButton) {
        animate(sender, transform: .identity)
    }

    private func animate(_ button: UIButton, transform: CGAffineTransform) {
        UIView.animate(withDuration: 0.4,
                       delay: 0,
                       usingSpringWithDamping: 0.5,
                       initialSpringVelocity: 3,
                       options: [.curveEaseInOut],
                       animations: {
                        button.transform = transform
        }, completion: nil)
    }
}

I am able to get this animation working, but it doesn't feel quite right. So I'd rather not use the code below. But it does work. So I'm not sure what in the code above is causing the problem.

extension UIButton {

    func pulsate() {

        let pulse = CASpringAnimation(keyPath: "transform.scale")
        pulse.duration = 0.2
        pulse.fromValue = 0.96
        pulse.toValue = 1.0
        pulse.repeatCount = 0
        pulse.initialVelocity = 0.5
        pulse.damping = 1.0

        layer.add(pulse, forKey: nil)
    }
}

Upvotes: 0

Views: 390

Answers (1)

user15154435
user15154435

Reputation: 46

I had the same issue and I solved unchecking the delay touch down button in the scrollview's attributes inspector : ScrollView's attrobutes inspector

Upvotes: 3

Related Questions