I’ve a view that I need to animate the next manner :
- Change view remodel Y scale all the way down to 0 utilizing
CGAffineTransform
- Change view background shade and scale view remodel again to .id
Since I need to reuse the identical easing perform, I wish to have just one UIViewPropertyAnimator
and use its isReversed
property.
Nevertheless once I do that, the end result appears good on display screen however the inside scale stays at 0.
Here is a easy ViewController that replicates the difficulty :
import UIKit
import Mix
class ViewController: UIViewController {
personal var testView: UIView = {
let view = UIView()
view.backgroundColor = .purple
return view
}()
personal lazy var scaleAnimator = UIViewPropertyAnimator(period: 2,
controlPoint1: CGPoint(x: 0.36, y: 0),
controlPoint2: CGPoint(x: 0.66, y: -0.56))
personal var cancellables = Set<AnyCancellable>()
override func viewDidLoad() {
tremendous.viewDidLoad()
view.addSubview(testView)
testView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
testView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
testView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
testView.widthAnchor.constraint(equalToConstant: 200),
testView.heightAnchor.constraint(equalToConstant: 200),
])
setupAnimation()
DispatchQueue.most important.asyncAfter(deadline: .now() + 1) {
self.startAnimation()
}
}
func setupAnimation() {
scaleAnimator.pausesOnCompletion = true
scaleAnimator.addAnimations {
self.testView.remodel = CGAffineTransform(scaleX: 1, y: 1e-10) // Scale animations to precisely 0.0 do not work
}
scaleAnimator.writer(for: .isRunning, choices: [.new])
.sink { [weak self] isRunning in
guard let self,
!isRunning else {
print("Animation begin")
print("Scale Y : (self!.testView.remodel.d)")
return
}
guard !self.scaleAnimator.isReversed else {
print("Animation finish")
print("Scale Y : (self.testView.remodel.d)")
return
}
print("Half animation")
print("Scale Y : (self.testView.remodel.d)")
self.testView.backgroundColor = .blue
self.scaleAnimator.isReversed = true
self.scaleAnimator.startAnimation()
}
.retailer(in: &cancellables)
}
func startAnimation() {
scaleAnimator.isReversed = false
scaleAnimator.startAnimation()
}
}
On the finish, this code prints that the Y scale is 0, however on display screen I’ve a 200px blue sq.. If I examine the view, its scale is 0.
What am I doing flawed right here ?