Blog / Apple Dev, , updated
Did you know that UIKit has a built-in physics engine?
UIKit includes a physics engine. It has physics fields and collisions, and it is reminiscent of what SpriteKit offers. From Apple Documentation:
One notable and common use of a dynamic item behavior is to confer a velocity to a dynamic item to match the ending velocity of a user gesture.
Apple has a sample project called StickyCorners that uses UIKit Dynamics. It presents a red rectangle that we can throw around with touch gestures, and the rectangle would stick to the nearest screen corner. I think these experiments grew into current iOS behaviors such as the picture-in-picture overlay.
StickyCorners dates back to 2016 and iOS 9. When opened in Xcode 16, Xcode says that it does not support the Swift version. But there is only one notable change to make, which is to add an objc prefix before some gesture functions, and the project will run perfectly.
Below is the code to enable live preview: set compatibility to iOS 17 or later, import SwiftUI, then use the #Preview macro on the view controller:
import UIKit
import SwiftUI
#Preview {
StickyCornersViewController()
}
class StickyCornersViewController: UIViewController {
//..
}
Update: on Xcode 27 and later, the project lifecycle must be migrated to the new UIKit scene-based lifecycle. See iOS App Lifecyle for a guide on lifecycle migration.
Links
-
Apple Documentation, UIDynamicItemBehavior.
-
Apple Documentation Archive, StickyCorners.
-
Achraf Kassioui, iOS App Lifecycle, 2025.