Animate UIKit views with SwiftUI animations in iOS 18

iOS 18 introduced a powerful new feature: the ability to animate UIKit views using SwiftUI animation types. This bridges the gap between the two frameworks even further, allowing us to bring the flexibility and expressiveness of SwiftUI animation system into UIKit-based projects.

Let’s take a look at a simple example to see how this works in practice. We’ll animate a UIImageView that scales up and down continuously.

Here is the initial setup:

class ViewController: UIViewController {
    private var animatingView: UIView?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        animatingView = UIImageView(
            image: UIImage(systemName: "volleyball.fill")
        )
        animatingView?.tintColor = .systemPink
        animatingView?.contentMode = .scaleAspectFit
        animatingView?.frame = CGRect(
            origin: .init(x: 0, y: 0),
            size: .init(width: 80, height: 80)
        )
        view.addSubview(animatingView!)
        animatingView?.center = view.center
    }
}

Next, we'll define the animation logic. The animation will start when the view appears on the screen, so we’ll implement it in the viewDidAppear() method. We'll use SwiftUI Animation API to create a linear animation lasting 1.3 seconds that repeats indefinitely. We'll apply this animation to the image view using the new UIView.animate() method, which accepts a SwiftUI Animation as an argument.

class ViewController: UIViewController {
    ...

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.startAnimating()
    }

    private func startAnimating() {
        let animation = SwiftUI.Animation
            .linear(duration: 1.3)
            .repeatForever()
        
        UIView.animate(animation) { [weak self] in
            self?.animatingView?.transform = .init(scaleX: 2, y: 2)
        }
    }
}

Finally, we can preview the animation directly in Xcode to ensure everything is working as expected:

#Preview {
    ViewController()
}

When the app runs, the volleyball icon smoothly scales up to twice its size and continuously repeats this effect.

A gif of a smooth animation of a volleyball growing and shrinking in the middle of the phone screen

SwiftUI animation API makes it simple to define animations and manage their timing and repetition. By using SwiftUI animations in UIKit, we can create smoother, more cohesive animations across our entire app, improving the overall experience for users.


If you have older iOS apps and want to enhance them with modern SwiftUI features, check out my book Integrating SwiftUI into UIKit Apps. It provides detailed guidance on gradually adopting SwiftUI in your UIKit projects. Additionally, if you're eager to enhance your Swift programming skills, my latest book Swift Gems offers over a hundred advanced tips and techniques, including optimizing collections, handling strings, mastering asynchronous programming, and debugging, to take your Swift code to the next level.

Integrating SwiftUI into UIKit Apps by Natalia Panferova book coverIntegrating SwiftUI into UIKit Apps by Natalia Panferova book cover

Enhance older apps with SwiftUI!$45

A detailed guide on gradually adopting SwiftUI in UIKit projects

Updated for iOS 18 and Xcode 16!

Integrating SwiftUI into UIKit Appsby Natalia Panferova

  • Upgrade your apps with new features like Swift Charts and Widgets
  • Support older iOS versions with effective backward-compatible strategies
  • Seamlessly bridge state and data between UIKit and SwiftUI using the latest APIs

Enhance older apps with SwiftUI!

A detailed guide on gradually adopting SwiftUI in UIKit projects

Integrating SwiftUI into UIKit Apps by Natalia Panferova book coverIntegrating SwiftUI into UIKit Apps by Natalia Panferova book cover

Integrating SwiftUI
into UIKit Apps

by Natalia Panferova

Updated for iOS 18 and Xcode 16!

$45