Run animations at high frame rates on iPhone 13 Pro

Overview

Run animations at high frame rates on iPhone 13 Pro

Background

When using a UIViewPropertyAnimator to animate objects on iPhone 13 Pro, CoreAnimation infers the frame rate that you need, without providing you with control over this frame rate.

Say you’re animating a rectangle quickly across the screen. It may decide to give you a smooth 120 FPS when the rectangle is moving fast (currently capped at 60 FPS due to a CoreAnimation bug). If you’re animating a button’s alpha, however, it may decide to give you a frame rate around 30 FPS (tested in instruments for a 0.4s alpha transition). This looks very choppy. For more information on this behaviour, see Optimizing ProMotion Refresh Rates for iPhone 13 Pro and iPad Pro.

It has already been observed that third party developers are unable to build animations exceeding 60 FPS using CoreAnimation on iPhone 13 Pro, but it should also be noted that developers cannot override lower refresh rate inferences either (i.e. a fading button running at 30 Hz), so more subtle fade animations will look choppy on this device.

I hope Apple addresses these CoreAnimation issues so that developers don’t have to manually work around them in the future, but for now, here is how you can run animations at your desired frame rate.

Solution

To run animations at high frame rates, you can use a “dummy” CADisplayLink that requests high frame rates using the preferredFrameRateRange property, but does not perform any tasks.

Add this entry to your Xcode project's info.plist

CADisableMinimumFrameDurationOnPhone
Type: Bool, Value: true

Paste this code into your Xcode Project.

///
/// An object that allows you to manually request an increased display refresh rate on ProMotion devices.
///
/// *The display refresh rate does not exceed 60 Hz when low power mode is enabled.*
///
/// **Do not set an excessive duration. Doing so will negatively impact battery life.**
///
class FrameRateRequest {
    
    private let frameRateRange: CAFrameRateRange
    private let duration: Double
    
    /// Prepares your frame rate request parameters.
    init(preferredFrameRate: Float, duration: Double) {
        frameRateRange = CAFrameRateRange(minimum: 30, maximum: Float(UIScreen.main.maximumFramesPerSecond), preferred: preferredFrameRate)
        self.duration = duration
    }
    
    /// Perform frame rate request.
    func perform() {
        let displayLink = CADisplayLink(target: self, selector: #selector(dummyFunction))
        displayLink.preferredFrameRateRange = frameRateRange
        displayLink.add(to: .current, forMode: .common)
        DispatchQueue.main.asyncAfter(deadline: .now() + duration) {
            displayLink.remove(from: .current, forMode: .common)
        }
    }
    
    @objc private func dummyFunction() {}
}

You can now submit a frame rate request like this:

// Attempts to increase the display refresh rate to 120 Hz for 0.4s (the expected duration of the animation).
let request = FrameRateRequest(preferredFrameRate: 120,
                               duration: 0.4)
request.perform()



// After performing the FrameRateRequest, run your animation(s) here using animators of your choice (UIViewPropertyAnimator, UIView.animate, etc).

You might also like...
Swift interpolation for gesture-driven animations
Swift interpolation for gesture-driven animations

Interpolate Interpolate is a powerful Swift interpolation framework for creating interactive gesture-driven animations. Usage The 🔑 idea of Interpola

An iOS library to natively render After Effects vector animations
An iOS library to natively render After Effects vector animations

Lottie for iOS, macOS (and Android and React Native) View documentation, FAQ, help, examples, and more at airbnb.io/lottie Lottie is a mobile library

A library to simplify iOS animations in Swift.

Updated for Swift 4.2 Requires Xcode 10 and Swift 4.2. Installation Drop in the Spring folder to your Xcode project (make sure to enable "Copy items i

Swift library for choreographing animations on the screen.
Swift library for choreographing animations on the screen.

Spruce iOS Animation Library (and Android) What is it? Spruce is a lightweight animation library that helps choreograph the animations on the screen.

A UICollectionViewLayout subclass that adds custom transitions/animations to the UICollectionView without effecting your existing code.
A UICollectionViewLayout subclass that adds custom transitions/animations to the UICollectionView without effecting your existing code.

AnimatedCollectionViewLayout Normally a UICollectionView has no transition effects when you scroll from one item to another. There are lots of ways to

Composable animations in Swift

Composable animations in Swift. Blog Installation Cocoapods The easiest way to get started is to use CocoaPods. Just add the following line to your Po

A port of SwiftUILab's Advanced Animations that also supports macOS
A port of SwiftUILab's Advanced Animations that also supports macOS

SwiftUILab Advanced Animations on the Mac as well A port of SwiftUILab's Advanced Animations that also supports macOS Here's the Ghist of the original

Animations created with SwiftUI.
Animations created with SwiftUI.

A repository containing a variety of animations and Animated components created in SwiftUI that you can use in your own projects.

☠️SkeletonUI aims to bring an elegant, declarative syntax to skeleton loading animations.
☠️SkeletonUI aims to bring an elegant, declarative syntax to skeleton loading animations.

SkeletonUI aims to bring an elegant, declarative syntax to skeleton loading animations. Get rid of loading screens or spinners and start using skeletons to represent final content shapes.

Owner
Duraid Abdul
WWDC20 Scholar, iOS Developer, Mechanical Engineering at UAlberta
Duraid Abdul
A collection of animations for iOS. Simple, just add water animations.

DCAnimationKit A collection of animations for iOS Simply, just add water! DCAnimationKit is a category on UIView to make animations easy to perform. E

Dalton 797 Sep 23, 2022
(Animate CSS) animations for iOS. An easy to use library of iOS animations. As easy to use as an easy thing.

wobbly See Wobbly in action (examples) Add a drop of honey ?? to your project wobbly has a bunch of cool, fun, and easy to use iOS animations for you

Sagaya Abdulhafeez 150 Dec 23, 2021
(Animate CSS) animations for iOS. An easy to use library of iOS animations. As easy to use as an easy thing.

wobbly See Wobbly in action (examples) Add a drop of honey ?? to your project wobbly has a bunch of cool, fun, and easy to use iOS animations for you

Sagaya Abdulhafeez 150 Dec 23, 2021
Simple Interface Core Animation. Run type-safe animation sequencially or parallelly

Simple Interface Core Animation Sica can execute various animations sequentially or parallelly. Features Animation with duration and delay parallel /

CATS Open Source Softwares 1k Nov 10, 2022
DynamicBlurView is a dynamic and high performance UIView subclass for Blur.

DynamicBlurView DynamicBlurView is a dynamic and high performance UIView subclass for Blur. Appetize's Demo Since using the CADisplayLink, it is a hig

Kyohei Ito 929 Jan 5, 2023
BWMCoverView is a very easy to use advertising the carousel view, supports circular scrolling functions such as switching, asynchronous loading of images, animation, custom is very high.

BWMCoverView BWMCoverView is a very easy to use advertising the carousel view, supports circular scrolling functions such as switching, asynchronous l

Bi Weiming 31 Mar 10, 2021
Physics-based animations for iOS, tvOS, and macOS.

Advance An animation library for iOS, tvOS, and macOS that uses physics-based animations (including springs) to power interactions that move and respo

Tim Donnelly 4.5k Dec 29, 2022
Ease is an event driven animation system that combines the observer pattern with custom spring animations as observers

Ease is an event driven animation system that combines the observer pattern with custom spring animations as observers. It's magic. Features Animate a

Robert-Hein Hooijmans 1.3k Nov 17, 2022
Advanced Natural Motion Animations, Simple Blocks Based Syntax

FlightAnimator Moved to Swift 3.1 Support: For Swift 3.1 - Use tag Version 0.9.9 See Installation Instructions for clarification Introduction FlightAn

Anton 589 Dec 29, 2022