Swift Animated Buttons jumping when first clicked

时间:2018-03-29 11:00:11

标签: ios swift animation button cgaffinetransform

I've got some animations on some buttons, when they're touched down they shrink and grow back when released.

The issue I'm facing is that when the view is loaded and the buttons are first touched they kind of jump (as illustrated below). When clicked again their behavior is as expected.

enter image description here

请注意旋转的HOLD圈不是按钮,而是叠加的图像视图,确认按钮只有问题。还要考虑到walletViewController的转换也没有发生,只要问题没有解决,就会停用。

@IBAction func buttonTapped(_ sender: UIButton) {
    UIView.animate(withDuration: 0.15) {
        self.badgeButton.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
        self.holdAnimateBadge.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
    }
    UIView.animate(withDuration:0.15) {
        let imageView = self.holdAnimateBadge
        imageView?.alpha = 0.5
    }
}

@IBAction func buttonToWallet(_ sender: Any) {
    UIView.animate(withDuration: 0.20) {
        self.badgeButton.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
        self.badgeButton.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
        self.holdAnimateBadge.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
        self.holdAnimateBadge.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
    }
    UIView.animate(withDuration:0.20) {
        let imageView = self.holdAnimateBadge
        imageView?.alpha = 1
    }
    if badgeButton.image(for: UIControlState.normal) == UIImage(named: "SellImage") {
        let walletViewController = self.storyboard?.instantiateViewController(withIdentifier: "walletViewController")
        self.navigationController?.pushViewController(walletViewController!, animated: true)
    } else if badgeButton.image(for: UIControlState.normal) == UIImage(named: "BuyImage") {
        let walletViewController = self.storyboard?.instantiateViewController(withIdentifier: "walletViewController")
        self.navigationController?.pushViewController(walletViewController!, animated: true)
    } else {
        return
    }
}

问题似乎与这个CGAffineTransform(scaleX: 1.5, y: 1.5)动画有关,并且两个动作同时被调用,但我不知道为什么只有在第一次点击时以及首次加载视图时才会发生这种动作

1 个答案:

答案 0 :(得分:0)

其中一个问题如下:当你像这样动画时:

UIView.animate(withDuration: 0.20) {
    self.badgeButton.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
    self.badgeButton.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
}

您将变换矩阵更改两次,因此第二次分配将覆盖第一次。因此,第一次转变将被忽略。

因此,我认为您必须查看animateKeyframesWithDuration,它允许您添加多个动画(例如缩小并立即再缩放)。