As of iOS 13.5, SwiftUI’s rotationEffect modifier takes the “long way around” when animating changes in rotation. For example, a rotation from 350° to 20° will cover 330° of rotation, even though those angles have only 30° of separation.
UIKit’s animation libraries handle this situation correctly. This Swift Package is a simple wrapper, accessible by modifier, that adds this UIKit rotation behavior to your SwiftUI view.
Installation
UIRotationEffect supports Swift Package Manager. Simply add this url in XCode > File > Swift Packages > Add Package Dependency:
https://github.com/DominicHolmes/UIRotationEffect
Usage
Import UIRotationEffect into your project. Then use .uiRotationEffect(angle) wherever you would normally use .rotationEffect(angle).
Because the animation is now handled by UIKit, you’ll have to pass in additional options if you want to customize it. uiRotationEffect takes all the same options as this method.
UIRotationEffect
Credit: @alonsoholmes for most of this solution.
Explanation
As of iOS 13.5, SwiftUI’s
rotationEffect
modifier takes the “long way around” when animating changes in rotation. For example, a rotation from 350° to 20° will cover 330° of rotation, even though those angles have only 30° of separation.UIKit’s animation libraries handle this situation correctly. This Swift Package is a simple wrapper, accessible by modifier, that adds this UIKit rotation behavior to your SwiftUI view.
Installation
UIRotationEffect supports Swift Package Manager. Simply add this url in XCode > File > Swift Packages > Add Package Dependency:
Usage
Import
UIRotationEffect
into your project. Then use.uiRotationEffect(angle)
wherever you would normally use.rotationEffect(angle)
.Because the animation is now handled by UIKit, you’ll have to pass in additional options if you want to customize it.
uiRotationEffect
takes all the same options as this method.The full method signature is:
Example usage:
Known Issues (Ideas Welcome)