You need to enable JavaScript to run this app. ARKit场景中如何实现物体随手指滑动绕中心旋转且不移动位置
优惠活动
大模型
产品
解决方案
定价
更多

ARKit场景中如何实现物体随手指滑动绕中心旋转且不移动位置

How to Get Your ARKit Cup to Rotate On-Swipe (Around Its Own Center)

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:

  1. Track how far the user swiped each frame
  2. Convert that swipe distance into a rotation angle
  3. Only rotate the cup while the swipe is active (i.e., when the finger is moving)
  4. 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.y modifies 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 .changed case:
    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.01 multiplier is a starting point. If the cup spins too slow, bump it up to 0.02; if it's too fast, drop it to 0.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:59:37