I want my moonImageView to keep its last size/dimension after the animation ended; image gets bigger by 1.25 time, but adding isRemoveOnCompletion = false
did not help. The size of image returned back after the animation ended.
How may I fix this? Did I misuse it?
//setup
moonImageView.contentMode = .scaleAspectFit
moonImageView.translatesAutoresizingMaskIntoConstraints = false
moonImageView.addTapGesture(tapNumber: 1, target: self, action: #selector(moonClick))
//layout
NSLayoutConstraint.activate([
moonImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
moonImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
moonImageView.widthAnchor.constraint(equalToConstant: 150),
moonImageView.heightAnchor.constraint(equalToConstant: 150)
])
//func
@objc private func moonClick() {
let moonAnimation = CABasicAnimation(keyPath: "transform.scale")
moonAnimation.fromValue = 1
moonAnimation.toValue = 1.25
moonAnimation.duration = 0.25
moonAnimation.isRemovedOnCompletion = false
moonImageView.layer.add(moonAnimation, forKey: nil)
}
CodePudding user response:
Forget the whole isRemovedOnCompletion
thing; that's just a canard. Of course the animation should be removed on completion; it's over. The problem is simply that you forgot to the layer's actual transform to the toValue
. (The animation is just sort of a visual illusion and has no effect on the layer's actual transform.)
CATransaction.setDisableActions(true)
moonImageView.layer.transform = CATransform3DMakeScale(1.25, 1.25, 1)