You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过函数调用停止用户交互触发的SceneView视角动画

实现SceneView点击停止相机交互的方案

当sceneView.allowsCameraControl = true时,要实现点击视图停止用户对相机视角的调整,可通过以下步骤完成:

核心逻辑

给SCNView(或ARSCNView)添加点击手势识别器,在点击触发时禁用相机控制权限,或锁定当前相机的变换状态,从而终止用户的交互操作。

具体实现

1. 添加点击手势识别器

在视图初始化或viewDidLoad中,为sceneView绑定点击手势:

override func viewDidLoad() {
    super.viewDidLoad()
    // 初始化sceneView并开启相机控制
    sceneView.allowsCameraControl = true
    
    // 添加点击手势
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onSceneViewTapped))
    tapGesture.cancelsTouchesInView = false
    sceneView.addGestureRecognizer(tapGesture)
}

2. 实现点击回调函数

根据需求选择以下两种方式之一:

方式一:直接禁用相机控制(简单直接)

点击后用户无法再调整相机视角,当前的相机位置/朝向会被保留:

@objc private func onSceneViewTapped() {
    sceneView.allowsCameraControl = false
}

方式二:锁定当前相机变换(支持后续恢复)

如果需要之后重新开启相机控制并保留当前视角,可先保存相机状态,再禁用控制:

private var lockedCameraTransform: SCNMatrix4?

@objc private func onSceneViewTapped() {
    guard let cameraNode = sceneView.pointOfView else { return }
    // 保存当前相机的变换参数
    lockedCameraTransform = cameraNode.transform
    // 停止交互
    sceneView.allowsCameraControl = false
}

// 如需恢复交互,可调用此方法
private func restoreCameraControl() {
    guard let transform = lockedCameraTransform, let cameraNode = sceneView.pointOfView else { return }
    cameraNode.transform = transform
    sceneView.allowsCameraControl = true
}

3. 处理手势冲突(可选)

若点击手势与相机自带的拖拽/旋转手势冲突,可通过手势代理解决:

extension YourViewController: UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        // 允许点击手势与相机控制手势同时触发
        return true
    }
}

记得给tapGesture.delegate = self。

补充说明

  • 对于ARKit的ARSCNView,上述逻辑完全适用,但需注意:禁用allowsCameraControl不会影响ARKit的环境跟踪,仅停止用户手动调整相机视角。
  • 如果要终止正在进行中的调整操作(比如用户正在拖拽旋转时点击),只需调用sceneView.allowsCameraControl = false,系统会立即终止当前的手势交互。

内容的提问来源于stack exchange,提问作者Hassan Taleb

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 09:25:22