2016-04-02 19 views
7

円の形をしており、次のコードで回転させてtouchesBegan(touches:, withEvent:)touchesMoved(touches:, withEvent:)を上書きします。回転速度を計算し、iOSビューを自然回転させます

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:)を上書きしたいです。私は現在、元の変換を保存し、デルタの角度を計算します。これをどのように実装できますか?前もって感謝します!

+1

速度は時間で除算されます。したがって、touchesBeganとtouchesEndにNSDate()。timesince1970を追加すると、beginイベントとendイベントの間の秒数を計算できます。距離は単にsqrt(dx^2 + dy^2)です。それともあなたが探している速度ではないのですか?それ以外の回答は以下のように書き直してください:) – Emptyless

+0

私は運動スクロールを処理してスクロールを続けるようにしていました。ユーザーが超高速スクロールしてから実際にスロースクロールすると、それは回転していた最後のスピードよりも速いスピードで回転し続けます。私は質問の速度に重点を置いたが、運動スクロールについてはすべて –

答えて

2

以下私の例では、CGAffineTransformRotateは依存する:

  • 方向(左からユーザが移動、アップまたはダウン等場合)
  • touchesBegan間の時間に依存回転(因子とtouchesEnded)
  • PI

これはCGAffineTransformRotateを作成し、これはUIViewAnimationOptスクロール運動の感じを作成するために、期間1(S)と共に回転しますions.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) 

} 

を使用し、touchesBeganで

var dateTouchesStarted : NSDate? 
var dateTouchesEnded : NSDate? 
var touchPointStart : CGPoint? 

iが追加touchesBegan機能のするCGPointの場所を追加されます。

self.touchPointStart = touchPoint 

上記の説明に従って変数を設定します。

+1

それは私の状況のた​​めに働くようにそれを微調整しなければならなかったが、感謝!あなたは賞金を得る! –

関連する問題