ARView中实现模型360°循环旋转动画及手势控制问题
ARKit模型360度循环旋转与手势保留问题解决
问题1:实现持续360度完整循环旋转
你的代码逻辑错误在于动画目标值设置混乱:先保存初始transform到变量,随后修改了模型的旋转,却把初始transform作为动画的to参数,导致动画仅在初始旋转和修改后的旋转之间往复,而非完整360度旋转。
修复方案(二选一)
方案1:使用ByAnimation(推荐)
通过定义旋转增量,让动画在当前变换基础上叠加360度旋转,循环执行即可持续转圈:
// 替换原动画代码 let rotationAnimation = ByAnimation( by: Transform(rotation: simd_quatf(angle: 2 * .pi, axis: [0, 1, 0])), duration: 3, bindTarget: .transform, repeatMode: .repeat, easing: .linear ) let animationResource = try! AnimationResource.generate(with: rotationAnimation) modelEntity.playAnimation(animationResource)
方案2:使用FromToAnimation
明确指定起始和结束的旋转值,确保动画覆盖完整360度:
// 替换原动画代码 let startRotation = modelEntity.transform.rotation // 基于起始旋转叠加360度 let endRotation = startRotation * simd_quatf(angle: 2 * .pi, axis: [0, 1, 0]) let rotationAnimation = FromToAnimation( from: Transform(rotation: startRotation), to: Transform(rotation: endRotation), duration: 3, bindTarget: .transform, repeatMode: .repeat, easing: .linear ) let animationResource = try! AnimationResource.generate(with: rotationAnimation) modelEntity.playAnimation(animationResource)
问题2:添加动画后保留手势控制功能
默认动画会直接覆盖模型的transform属性,与手势的变换修改冲突。可通过两种方式解决:
方案1:开启动画叠加模式
给动画添加additive: true参数,让动画的旋转变换叠加在现有transform上,而非覆盖,这样手势的平移、旋转、缩放操作都能生效:
let rotationAnimation = ByAnimation( by: Transform(rotation: simd_quatf(angle: 2 * .pi, axis: [0, 1, 0])), duration: 3, bindTarget: .transform, repeatMode: .repeat, easing: .linear, additive: true // 关键:启用叠加模式 ) let animationResource = try! AnimationResource.generate(with: rotationAnimation) modelEntity.playAnimation(animationResource)
方案2:分离动画与手势的作用节点
创建一个容器节点承载动画,手势绑定到模型本身,两者互不干扰:
// 调整节点结构:创建动画容器 let animationContainer = Entity() animationContainer.addChild(modelEntity) anchorEntity.addChild(animationContainer) // 给容器添加旋转动画 let rotationAnimation = ByAnimation( by: Transform(rotation: simd_quatf(angle: 2 * .pi, axis: [0, 1, 0])), duration: 3, bindTarget: .transform, repeatMode: .repeat, easing: .linear ) let animationResource = try! AnimationResource.generate(with: rotationAnimation) animationContainer.playAnimation(animationResource) // 手势仍绑定到模型实体 installGestures(for: modelEntity)
内容的提问来源于stack exchange,提问作者Settasit7
相关产品推荐
相关产品推荐

