marcosgriselli / ViewAnimator

ViewAnimator brings your UI to life with just one line
MIT License
7.3k stars 488 forks source link

fixed transform not preserved with multiple animations #18

Closed cpwhidden closed 6 years ago

cpwhidden commented 6 years ago

The feature to preserve the view's transform after animating wouldn't work when using multiple Animations. That's because the animations.forEach loop in the animate function in ViewAnimator.swift reassigns preTransform for each animation provided to the animations array. So this only works correctly when there is only one Animation in the array. Otherwise, it would be set to the second-to-last transform from the entire animation sequence.

I noticed preTransform was set and retrieved using a computed property and using an Obj-C associated object. I didn't see how else this property could be used, so I think that just "saving" the constant at the beginning of the animate function and then restoring by capturing this constant in the UIView.animate completion handler is cleaner and safer here. Adding the preTransform property to all UIView might also interfere with clients' subclasses if they used such a property. As a bonus, this also makes the Constants.swift file unnecessary.

I added a test case for this scenario in the example app's test file, since I didn't see any tests for the pod.