2017-02-02 4 views
0

セグメント化されたコントロールがあり、コントロールの右側がタップされている場合は時計回りに、左側がタップされている場合は反時計回りに回転するようUIViewをアニメーションしたいと思います。回転する私のコードは次のとおりです。反時計回りにアニメートする方法

func rotateRight() { 
    UIView.animate(withDuration: 0.5, animations: { 
     self.profileImageView.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat(M_PI))/180.0) 
     self.profileImageView.transform = CGAffineTransform(rotationAngle: (0.0 * CGFloat(M_PI))/360.0) 
    }) 

そして私は、セグメントがタップされたかに応じて変更を処理するために、このコードを使用しています(更新のために編集):

if loginRegisterSegmentedControl.selectedSegmentIndex == 0 { 
     self.loginRegisterButton.backgroundColor = UIColor.blue 
     loginRegisterSegmentedControl.tintColor = UIColor.blue 
     rotateLeft() 
     // Roll to the left 
    } else { 
     self.loginRegisterButton.backgroundColor = UIColor.red 
     loginRegisterSegmentedControl.tintColor = UIColor.red 
     rotateRight() 
     // Roll to the right 
    } 

をだから、基本的にあれば、条件付きで私はrotateLeft()機能に電話したいと思います - どうすればいいですか?

編集:ここではピアースの提案など、アニメーションのための私の完全なコード、です:

// Rotation 
var viewAngle: CGFloat = 0 // Right-side up to start 
let π = CGFloat.pi // Swift allows special characters hold alt+p to use this, it allows for cleaner code 

func rotate(by angle: CGFloat) { 

    self.viewAngle += angle 

    UIView.animate(withDuration: 0.5, animations: { 
     self.profileImageView.transform = CGAffineTransform(rotationAngle: self.viewAngle) 
     self.view.layoutIfNeeded() 
    })   

} 

lazy var profileImageView: UIImageView = { 
    let imageView = UIImageView() 
    imageView.image = UIImage(named: "TTTdude") 
    imageView.translatesAutoresizingMaskIntoConstraints = false 
    imageView.contentMode = .scaleAspectFill 

    imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(rotate))) 
    imageView.isUserInteractionEnabled = true 

    return imageView 
}() 



lazy var loginRegisterSegmentedControl: UISegmentedControl = { 
    let sc = UISegmentedControl(items: ["iOS", "Android"]) 
    sc.translatesAutoresizingMaskIntoConstraints = false 
    sc.tintColor = UIColor.black 
    sc.selectedSegmentIndex = 0 
    sc.addTarget(self, action: #selector(handleLoginRegisterChange), for: .valueChanged) 
    return sc 
}() 

func handleLoginRegisterChange() { 

    let title = loginRegisterSegmentedControl.titleForSegment(at: loginRegisterSegmentedControl.selectedSegmentIndex) 
    loginRegisterButton.setTitle(title, for: .normal) 

    if loginRegisterSegmentedControl.selectedSegmentIndex == 0 { 
     self.loginRegisterButton.backgroundColor = UIColor.blue 
     loginRegisterSegmentedControl.tintColor = UIColor.blue 
     rotate(by: -2*π) 
     // Roll to the left 
    } else { 
     self.loginRegisterButton.backgroundColor = UIColor.red 
     loginRegisterSegmentedControl.tintColor = UIColor.red 
     rotate(by: 2*π) 
     // Roll to the right 
    } 

答えて

3

はちょうどあなたが時計回りのためにやったどんな角度でそれを回転させるが、しかし、負の1

func rotateLeft() { 
    UIView.animate(withDuration: 0.5, animations: { 
     self.profileImageView.transform = CGAffineTransform(rotationAngle: ((180.0 * CGFloat(M_PI))/180.0) * -1) 
     self.profileImageView.transform = CGAffineTransform(rotationAngle: ((0.0 * CGFloat(M_PI))/360.0) * -1) 
     self.view.layoutIfNeeded() 
    }) 
} 

Iを掛け(180.0 * CGFloat(π))/180と言ったときにそれを言及すべきです - 180/180が1であるので、あなたはπと言っています。

あなたは一度に2つの異なる回転をしようとしているように見えますか?私はそれで少し混乱しています。あなたが時計回りと反時計回りの両方で複数の回転をしている場合、ビューの軸方向を追跡することが本当に役に立ちました。あなたは正の角度で右のパスを回転させたい場合(つまり、π/ 4、π/ 2、π2 *π)viewAngle

var viewAngle: CGFloat = 0 // Right-side up to start 
let π = CGFloat.pi // Swift allows special characters hold alt+p to use this, it allows for cleaner code 

func rotate(by angle: CGFloat) { 

    for i in 0 ..< 4 { 
     UIView.animate(withDuration: 0.125, delay: 0.125 * Double(i), options: .curveLinear, animations: { 

      self.viewAngle += angle/4 
      self.rotateView.transform = CGAffineTransform(rotationAngle: self.viewAngle) 
      self.view.layoutIfNeeded() 
     }, completion: nil) 
    }   

} 

というプロパティを作成、または負の左側のパスの場合のような何かを角度。 180度左

rotate(by: π) 

を回し:

rotate(by: -π) 

EDIT:あなたが言及したように、私は自分のためにしようとするまで、私は見ていなかった、右180度

回し負か正かにかかわらず、πまたは2πを差し込むと、時計回りにしか回転しません。これを回避するには、-π/ 4刻みで回転させる必要がありました。だから完全な円を反時計回りに回転させるには、意味があるなら-π/ 4アニメーションのループを作った。それは動作しますが、これを行う簡単な方法があるはずです。

+0

ちょうど好奇心から - なぜあなたは180 *π/ 180を使用しますか?それはちょうどπ – Pierce

+0

ありがとう、それは素敵でシンプルでした - しかし、私は条件付き( 'if'は 'rotateLeft()'と 'else'''が' rotateRight() 'を持っています)を置き換えましたが、アニメーションに変更はありません左または右のセグメントをタップすると、常に時計回りに回転します。 – KingTim

+0

ちょうど 'view.transform = CGAffineTransform(rotationAngle:-π)'と答えてください。 – Pierce

1

私はビューを時計回りに360°回転させ、次に反時計回りに360°回転させて別の機能(選択したセグメントに応じて呼び出すことができます)を表示する方法はありませんか?もちろん

方法があります:

enter image description here

あなたが任意のビューでそれを行うことができます。回転ラベルは一例に過ぎません。もちろん、2つのボタンは必要ありません。私は2つの機能をどのように起動しているかを示すためにそれを行うだけです。

関連する問題