【发布时间】:2016-07-21 22:32:11
【问题描述】:
我有一个圆形视图,并通过以下代码覆盖touchesBegan(touches:, withEvent:) 和touchesMoved(touches:, withEvent:) 使其旋转。
var deltaAngle = CGFloat(0)
var startTransform: CGAffineTransform?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
let touchPoint = touches.first!.locationInView(view)
let dx = touchPoint.x - view.center.x
let dy = touchPoint.y - view.center.y
deltaAngle = atan2(dy, dx)
startTransform = arrowPickerView.transform
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
{
let touchPoint = touches.first!.locationInView(view)
let dx = touchPoint.x - view.center.x
let dy = touchPoint.y - view.center.y
let angle = atan2(dy, dx)
let angleDifference = deltaAngle - angle
let transform = CGAffineTransformRotate(startTransform!, -angleDifference)
arrowPickerView.transform = transform
}
我想覆盖touchesEnded(touches:, withEvent:) 来计算速度并让视图自然旋转一点(类似于连续滚动)。我目前保存原始变换并计算增量角度。我该如何实施?提前致谢!
【问题讨论】:
-
速度是距离除以时间,因此如果您在 touchesBegan 和 touchesEnd 中添加 NSDate().timesince1970,您可以计算开始事件和结束事件之间的秒数差异。距离只是 sqrt(dx^2 + dy^2)。或者这不是您正在寻找的速度?否则我会重写为下面的答案:)
-
我只是想处理动态滚动以使其继续滚动。如果用户说滚动速度超快然后真的很慢然后放手,那么它将继续以比上次旋转速度更快的速度旋转。我在问题中强调了速度,但这完全是关于动态滚动
标签: ios swift transform trigonometry touch-event