使用CASpringAnimation时为什么我的图像没有动画?

时间:2018-07-16 04:44:47

标签: swift xcode

我希望在点击图像时对其进行动画处理,到目前为止,这是我的代码,当我点击图像时由于某种原因什么也没发生。

lazy var profileImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.image = UIImage(named: "made")
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.contentMode = .scaleAspectFill
    imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(taptap)))
    imageView.isUserInteractionEnabled = true
    return imageView
}()

@objc func taptap() {
    let pulse = CASpringAnimation(keyPath: "transform.scale")
    pulse.duration = 0.6
    pulse.fromValue = 0.95
    pulse.toValue = 1.0
    pulse.autoreverses = true
    pulse.repeatCount = 2
    pulse.initialVelocity = 0.5
    pulse.damping = 1.0
    layer.add(pulse, forKey: nil)
}

1 个答案:

答案 0 :(得分:0)

除了在ImageView行中添加动画之外,其他所有内容都看起来不错。替换此行:

layer.add(pulse, forKey: nil)

使用:

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

您需要在视图的Core Animations中添加layer,这里是您的profileImageView