write code for rotation in UIViewController

Generated on 8/2/2024

1 search

To implement rotation in a UIViewController, you can use the following code snippet. This example demonstrates how to rotate a view by 90 degrees:

import UIKit

class RotationViewController: UIViewController {
    
    let rotatingView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Setup the view
        rotatingView.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        rotatingView.backgroundColor = .red
        view.addSubview(rotatingView)
        
        // Add a button to trigger the rotation
        let rotateButton = UIButton(type: .system)
        rotateButton.frame = CGRect(x: 100, y: 300, width: 100, height: 50)
        rotateButton.setTitle("Rotate", for: .normal)
        rotateButton.addTarget(self, action: #selector(rotateView), for: .touchUpInside)
        view.addSubview(rotateButton)
    }
    
    @objc func rotateView() {
        UIView.animate(withDuration: 0.5) {
            self.rotatingView.transform = self.rotatingView.transform.rotated(by: .pi / 2)
        }
    }
}

This code sets up a UIViewController with a red square view and a button. When the button is pressed, the red square rotates by 90 degrees (π/2 radians).

For more advanced animations and transitions, you might want to check out the session Enhance your UI animations and transitions from WWDC 2024, which covers various techniques for animating UIKit and SwiftUI views.

Render Metal with passthrough in visionOS

Render Metal with passthrough in visionOS

Get ready to extend your Metal experiences for visionOS. Learn best practices for integrating your rendered content with people’s physical environments with passthrough. Find out how to position rendered content to match the physical world, reduce latency with trackable anchor prediction, and more.

Explore game input in visionOS

Explore game input in visionOS

Discover how to design and implement great input for your game in visionOS. Learn how system gestures let you provide frictionless ways for players to interact with your games. And explore best practices for supporting custom gestures and game controllers.

Enhance your UI animations and transitions

Enhance your UI animations and transitions

Explore how to adopt the zoom transition in navigation and presentations to increase the sense of continuity in your app, and learn how to animate UIKit views with SwiftUI animations to make it easier to build animations that feel continuous.

Optimize your 3D assets for spatial computing

Optimize your 3D assets for spatial computing

Dive into an end-to-end workflow for optimized 3D asset creation. Discover best practices for optimizing meshes, materials, and textures in your digital content creation tool. Learn how to harness shader graph, baking, and material instances to enhance your 3D scene while optimizing performance. Take advantage of native tools to work more effectively with your assets and improve your app’s performance.

Create custom visual effects with SwiftUI

Create custom visual effects with SwiftUI

Discover how to create stunning visual effects in SwiftUI. Learn to build unique scroll effects, rich color treatments, and custom transitions. We’ll also explore advanced graphic effects using Metal shaders and custom text rendering.

Discover RealityKit APIs for iOS, macOS and visionOS

Discover RealityKit APIs for iOS, macOS and visionOS

Learn how new cross-platform APIs in RealityKit can help you build immersive apps for iOS, macOS, and visionOS. Check out the new hover effects, lights and shadows, and portal crossing features, and view them in action through real examples.

Compose interactive 3D content in Reality Composer Pro

Compose interactive 3D content in Reality Composer Pro

Discover how the Timeline view in Reality Composer Pro can bring your 3D content to life. Learn how to create an animated story in which characters and objects interact with each other and the world around them using inverse kinematics, blend shapes, and skeletal poses. We’ll also show you how to use built-in and custom actions, sequence your actions, apply triggers, and implement natural movements.

Dive deep into volumes and immersive spaces

Dive deep into volumes and immersive spaces

Discover powerful new ways to customize volumes and immersive spaces in visionOS. Learn to fine-tune how volumes resize and respond to people moving around them. Make volumes and immersive spaces interact through the power of coordinate conversions. Find out how to make your app react when people adjust immersion with the Digital Crown, and use a surrounding effect to dynamically customize the passthrough tint in your immersive space experience.