当用户停止捏住时,我尝试使用UIPinchGestureRecognizer
的属性velocity
在SceneKit节点中播放动画,但是我不了解velocity
的属性如何计算/应该使用。
这是我的代码:
var initialScale = simd_float3(1)
@objc private func handlePinch(sender: UIPinchGestureRecognizer) {
let scale = simd_float3(Float(sender.scale))
let state = sender.state
let vel = Float(sender.velocity)
if state == .began {
updateQ.async(group: updateGrp) {
log.debug("Begin scale: \(scale)")
log.debug("Begin vel: \(vel)")
if let node = self.activeNode {
self.initialScale = node.simdScale
}
else {
self.initialScale = self.layeredImgNode.simdScale
}
}
}
else if state == .changed {
updateQ.async(group: updateGrp) {
log.debug("Changed scale: \(scale)")
log.debug("Changed vel: \(vel)")
let scale = self.initialScale * scale
if let node = self.activeNode {
node.simdScale = scale
}
else {
self.layeredImgNode.simdScale = scale
}
}
}
else if state == .ended {
updateQ.async(group: updateGrp) {
log.debug("End scale: \(scale)")
log.debug("End vel: \(vel)")
// Calculate for how long to run the animation
let time = Double(vel / Float(10))
let target = self.initialScale * (scale + simd_float3(vel))
log.debug("End target: \(target) and time: \(time)")
self.initialScale = simd_float3(1)
SCNTransaction.begin()
SCNTransaction.animationDuration = time
if let node = self.activeNode {
node.simdScale = target
}
else {
self.layeredImgNode.simdScale = target
}
SCNTransaction.commit()
}
}
文档指出velocity
为“捏合速度每秒的比例因子”。但是,我仍然不知道如何使用它。有人可以帮我吗?
谢谢。