RealityKit & ARKit in SwiftUI — Tap to Swap Item
While developing for the Augmented Reality, we can interact with virtual objects by tapping on them, and today we will add the functionality to tap and swap items. We will include a few items from Apple’s provided USDZ models, and as the user taps on an item, it will be replaced with the next one in the list, cycling through all the items.
Download sample USDZs from the link below:
Next, we will add them into the project by dragging and dropping into the project
Create a new file named “ItemListEnum” to hold asset names in an enum format.
import Foundation
enum ItemListEnum: String, CaseIterable {
case chairSwan = "chair_swan"
case fenderStratocaster = "fender_stratocaster"
case gramophone = "gramophone"
case tvRetro = "tv_retro"
}
We’ll utilize a tap gesture to detect user taps on the AR item and swap it with the next item. Since we’re using UIViewRepresentable, the coordinator handles target and actions. Acting as a mediator, the Coordinator in SwiftUI's UIViewRepresentable…