ARKit场景中如何实现物体随手指滑动绕中心旋转且不移动位置
Got it, let's walk through exactly how to make your ARKit cup rotate around its own center when you swipe—stopping as soon as you lift your finger, no position shifts involved. This is a super common interaction, and the implementation is straightforward once you know the key bits.
First: Add a Pan Gesture Recognizer
We'll use a UIPanGestureRecognizer to track the user's swipe movement. Attach it to your AR view like this:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handleSwipeRotation(_:))) arView.addGestureRecognizer(panGesture)
Next: Handle the Swipe and Apply Rotation
The magic happens in the gesture handler. We need to:
- Track how far the user swiped each frame
- Convert that swipe distance into a rotation angle
- Only rotate the cup while the swipe is active (i.e., when the finger is moving)
- Make sure it spins around its own center, not the world origin
Here's the code you'll need:
// Assume you have a reference to your cup node stored as `cupNode` @objc func handleSwipeRotation(_ gesture: UIPanGestureRecognizer) { guard let cup = cupNode else { return } // Get how much the finger moved since the last frame let swipeDelta = gesture.translation(in: arView) // Convert horizontal swipe (X-axis) to a rotation angle // Tweak the 0.01 multiplier to adjust rotation speed—higher = faster let rotationAmount = swipeDelta.x * 0.01 switch gesture.state { case .changed: // Rotate the cup around its local Y-axis (spin left/right) cup.eulerAngles.y += rotationAmount // Reset the delta so we get incremental changes each time the handler fires gesture.setTranslation(.zero, in: arView) case .ended, .cancelled: // Stop rotation immediately when the finger lifts or swipe is canceled break default: break } }
Key Details to Get Right
- Local vs. World Rotation: Using
eulerAngles.ymodifies the cup's local rotation, which ensures it spins around its own center—no matter where it's placed in the AR scene. If you used world rotation instead, it would spin around the scene's origin, which is not what you want. - Adjusting for Vertical Rotation: If you want swiping up/down to rotate the cup forward/backward, just add this line in the
.changedcase:cup.eulerAngles.x += swipeDelta.y * 0.01 - Fixing Pivot Issues: If your cup is spinning around a weird point (not its center), your model's pivot might be off. Fix it by setting the pivot to the center of the cup's bounding box:
let boundingBox = cup.boundingBox cup.pivot = SCNMatrix4MakeTranslation( (boundingBox.min.x + boundingBox.max.x)/2, (boundingBox.min.y + boundingBox.max.y)/2, (boundingBox.min.z + boundingBox.max.z)/2 ) - Rotation Speed: The
0.01multiplier is a starting point. If the cup spins too slow, bump it up to0.02; if it's too fast, drop it to0.005.
Test It Out
Fire up your app, place the cup in your AR space, and swipe horizontally—you should see it spin smoothly around its center only while your finger is moving. Lift your finger, and it stops instantly. Perfect!
内容的提问来源于stack exchange,提问作者meowsush

