2017-02-11 13 views
0

これは私の問題です。どのボタンをタップしても中心になるはずですが、私はCGAffineTransformとCABasicAnimationを使用していますが、回転する角度を得ることができません。それらは円形に、任意の助けを回転角度を見つける2点

func rotateBarrel(with duration:CGFloat,angle:Double) { 
    let rotationAnimaton = CABasicAnimation(keyPath: "transform.rotation.z") 
    rotationAnimaton.toValue = angle 

    rotationAnimaton.duration = CFTimeInterval(duration) 
    barrelContainingView.layer.add(rotationAnimaton, forKey: "rotationAnimation") 
    barrelContainingView.transform = CGAffineTransform(rotationAngle: CGFloat(angle)) 
} 

コード角度計算のため

func angleToPoint(comparisonPoint: CGPoint) -> Double { 
     let originPoint = CGPoint(x:comparisonPoint.x-initialCenterPoint.x,y:comparisonPoint.y-initialCenterPoint.y) 
     return Double(atan2f(Float(originPoint.y), Float(originPoint.x))) 

    } 

//イニシアためenter image description here

コードを理解されるボタンです。 l中心点は回転前の3番目のボタンの中心です

答えて

0

関数atan2が必要です。デルタXとデルタYを中心点から点1に渡して、最初の角度を取得します。その後、2番目の点を繰り返して2番目の角度を取得し、角度の差を取ってください。それは円弧の角度です。

Githubには、この同じ数学を使ったワンタッチタップジェスチャ認識機能が含まれています(ジェスチャ認識機能では、指の始点からドラッグする点までの角度の変化を計算します)。 )

はここに関連するコードです:

let center = CGPoint(x: view!.bounds.midX, y: view!.bounds.midY) 
let currentTouchPoint = touch.location(in: view) 
let previousTouchPoint = touch.previousLocation(in: view) 

//Calculate the angle from the center of the view to the previous touch point. 
let previousAngle = atan2f(Float(previousTouchPoint.y - center.y), 
          Float(previousTouchPoint.x - center.x)) 
//Calculate the angle from the center of the view to the current touch point. 
let currentAngle = atan2f(Float(currentTouchPoint.y - center.y), 
          Float(currentTouchPoint.x - center.x)) 

//Adjust the rotation by the change in angle. 
rotation -= CGFloat(currentAngle - previousAngle) 

それはあなたのために働くの適応が必要になりますが、それはあなたの基本的な考え方を示すべきです。

関連する問題