Big*_*Mac 8 trigonometry transform touch-event ios swift
我有一个圆形的视图,并通过以下代码覆盖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
}
Run Code Online (Sandbox Code Playgroud)
我想覆盖touchesEnded(touches:, withEvent:)
以计算速度并使视图自然旋转一点(类似于连续滚动).我目前保存原始变换并计算delta角度.我该如何实现呢?提前致谢!
在我下面的示例中, CGAffineTransformRotate 取决于:
这将创建一个 CGAffineTransformRotate 并以持续时间 1 (s) 旋转,以创建动态滚动的感觉,使用 UIViewAnimationOptions.CurveEaseOut 属性
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touchPointEnd = touches.first!.locationInView(view)
self.dateTouchesEnded = NSDate()
let delay : NSTimeInterval = NSTimeInterval(0)
let timeDelta : Double = self.dateTouchesEnded!.timeIntervalSince1970 - self.dateTouchesStarted!.timeIntervalSince1970
let horizontalDistance : Double = Double(touchPointEnd.x - self.touchPointStart!.x)
let verticalDistance : Double = Double(touchPointEnd.y - self.touchPointStart!.y)
var direction : Double = 1
if (fabs(horizontalDistance) > fabs(verticalDistance)) {
if horizontalDistance > 0 {
direction = -1
}
} else {
if verticalDistance < 0 {
direction = -1
}
}
let rotation : Double = (0.1 / timeDelta < 0.99) ? 0.1 / timeDelta : 0.99
let duration : NSTimeInterval = NSTimeInterval(1)
UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseOut, animations: {
let transform = CGAffineTransformRotate(self.imageView.transform, CGFloat(direction) * CGFloat(rotation) * CGFloat(M_PI))
self.imageView.transform = transform
}, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
我添加了变量来跟踪触摸的开始和结束时间,并添加了 touchesBegan 函数的 CGPoint 位置
var dateTouchesStarted : NSDate?
var dateTouchesEnded : NSDate?
var touchPointStart : CGPoint?
Run Code Online (Sandbox Code Playgroud)
在 touchesBegan 中,我补充说:
self.touchPointStart = touchPoint
Run Code Online (Sandbox Code Playgroud)
如上所述设置变量。
归档时间: |
|
查看次数: |
1217 次 |
最近记录: |