Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
194: Hand Tracking and Gestures in visionOS
Hand tracking in visionOS is where the experience stops feeling like a floating iPad and starts feeling like actual spatial computing. We're moving beyond simple buttons and into the realm of direct manipulation. To get a feel for this, I want us to build a "Gravity Orb"—a simple 3D sphere that reacts when you "pinch" it in mid-air, changing its color and pulsing in size.
Creating the Interactive Orb
First, we need a 3D object to interact with. I'm using a RealityView here because it gives us the direct bridge to RealityKit entities. I'll start by creating a simple sphere with a metallic material. I like the metallic look because it catches the simulated light in the visionOS environment, making it easier to judge depth when you're reaching for it.
import SwiftUI
import RealityKit
struct GravityOrbView: View {
var body: some View {
RealityView { content in
let orb = ModelEntity(
mesh: .generateSphere(radius: 0.1),
materials: [SimpleMaterial(color: .systemBlue, isMetallic: true)]
)
orb.name = "gravity_orb"
content.add(orb)
}
}
}
Wiring up the Spatial Gesture
Now, we want the orb to react. In visionOS, we use SpatialTapGesture for those pinch-to-select actions. I'll attach the gesture to the RealityView and use the entity parameter to figure out exactly what the user is looking at and pinching.
// Inside the RealityView
.gesture(
SpatialTapGesture()
.targetedToAnyEntity()
.onEnded { value in
let entity = value.entity
// I want to change the color to green and scale it up
if let model = entity as? ModelEntity {
model.model?.materials = [SimpleMaterial(color: .systemGreen, isMetallic: true)]
model.scale = [1.2, 1.2, 1.2]
}
}
)
Why isn't it reacting?
I just ran this in the simulator, and... nothing. I'm pinching the orb, the system knows I'm tapping, but the onEnded closure never fires. This is a classic "gotcha" in visionOS development that almost every developer hits the first time.
The problem is that a ModelEntity is just visual geometry. For a gesture to "hit" an entity, that entity needs a physical presence in the physics world. If it doesn't have a CollisionComponent, it's essentially a ghost—the gesture passes right through it. I forgot to give the orb a shape for the system to collide with.
Here is the fix. I need to generate a collision shape that matches the sphere's radius:
// Add this inside the RealityView setup, right after creating the orb
orb.generateCollisionShapes(recursive: true)
Now, when I run it again, the pinch is registered immediately. It's a small line of code, but it's the difference between a broken app and a working one.
Adding a bit of polish
Changing the color instantly is a bit jarring. To make it feel more "organic," I'll use a simple animation block. Since RealityKit entities don't use SwiftUI animations, we have to handle the transition of the scale and material carefully. I'll use a basic move(to:relativeTo:duration:) call to make the orb "pulse" when pinched, which feels much more like a physical reaction than a snap.
.onEnded { value in
let entity = value.entity
if let model = entity as? ModelEntity {
model.model?.materials = [SimpleMaterial(color: .systemGreen, isMetallic: true)]
// Pulse effect: Scale up, then we could use a timer or
// separate animation to scale it back down.
var transform = entity.transform
transform.scale = [1.2, 1.2, 1.2]
entity.move(to: transform, relativeTo: entity.parent, duration: 0.2, timingFunction: .easeOut)
}
}
By combining SpatialTapGesture with CollisionComponent and basic Entity transforms, we've created a basic interactive object. The key takeaway here is that in visionOS, "seeing" is not the same as "interacting." Always check your collisions.
📋 Practical Task
Build a "Virtual Doorbell" Interaction
Create a small visionOS project featuring a 3D cube (the "doorbell") using RealityView. Implement the following functionality:
- The cube should start with a red color.
- Ensure the cube has the necessary components to be interactive (remember the lesson's "ghost" mistake!).
- Implement a
SpatialTapGestureso that when the user pinches the cube, it changes color to gold and momentarily shifts its position slightly backward (along the Z-axis) to simulate a button being pressed. - Use the
move(to:relativeTo:duration:)method to make the button press feel smooth rather than instantaneous.
There are no comments for now.