Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. func rotateRight() {
  2. UIView.animate(withDuration: 0.5, animations: {
  3. self.profileImageView.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat(M_PI)) / 180.0)
  4. self.profileImageView.transform = CGAffineTransform(rotationAngle: (0.0 * CGFloat(M_PI)) / 360.0)
  5. })
  6.  
  7. if loginRegisterSegmentedControl.selectedSegmentIndex == 0 {
  8. self.loginRegisterButton.backgroundColor = UIColor.blue
  9. loginRegisterSegmentedControl.tintColor = UIColor.blue
  10. rotateLeft()
  11. // Roll to the left
  12. } else {
  13. self.loginRegisterButton.backgroundColor = UIColor.red
  14. loginRegisterSegmentedControl.tintColor = UIColor.red
  15. rotateRight()
  16. // Roll to the right
  17. }
  18.  
  19. // Rotation
  20. var viewAngle: CGFloat = 0 // Right-side up to start
  21. let π = CGFloat.pi // Swift allows special characters hold alt+p to use this, it allows for cleaner code
  22.  
  23. func rotate(by angle: CGFloat) {
  24.  
  25. self.viewAngle += angle
  26.  
  27. UIView.animate(withDuration: 0.5, animations: {
  28. self.profileImageView.transform = CGAffineTransform(rotationAngle: self.viewAngle)
  29. self.view.layoutIfNeeded()
  30. })
  31.  
  32. }
  33.  
  34. lazy var profileImageView: UIImageView = {
  35. let imageView = UIImageView()
  36. imageView.image = UIImage(named: "TTTdude")
  37. imageView.translatesAutoresizingMaskIntoConstraints = false
  38. imageView.contentMode = .scaleAspectFill
  39.  
  40. imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(rotate)))
  41. imageView.isUserInteractionEnabled = true
  42.  
  43. return imageView
  44. }()
  45.  
  46.  
  47.  
  48. lazy var loginRegisterSegmentedControl: UISegmentedControl = {
  49. let sc = UISegmentedControl(items: ["iOS", "Android"])
  50. sc.translatesAutoresizingMaskIntoConstraints = false
  51. sc.tintColor = UIColor.black
  52. sc.selectedSegmentIndex = 0
  53. sc.addTarget(self, action: #selector(handleLoginRegisterChange), for: .valueChanged)
  54. return sc
  55. }()
  56.  
  57. func handleLoginRegisterChange() {
  58.  
  59. let title = loginRegisterSegmentedControl.titleForSegment(at: loginRegisterSegmentedControl.selectedSegmentIndex)
  60. loginRegisterButton.setTitle(title, for: .normal)
  61.  
  62. if loginRegisterSegmentedControl.selectedSegmentIndex == 0 {
  63. self.loginRegisterButton.backgroundColor = UIColor.blue
  64. loginRegisterSegmentedControl.tintColor = UIColor.blue
  65. rotate(by: -2*π)
  66. // Roll to the left
  67. } else {
  68. self.loginRegisterButton.backgroundColor = UIColor.red
  69. loginRegisterSegmentedControl.tintColor = UIColor.red
  70. rotate(by: 2*π)
  71. // Roll to the right
  72. }
  73.  
  74. func rotateLeft() {
  75. UIView.animate(withDuration: 0.5, animations: {
  76. self.profileImageView.transform = CGAffineTransform(rotationAngle: ((180.0 * CGFloat(M_PI)) / 180.0) * -1)
  77. self.profileImageView.transform = CGAffineTransform(rotationAngle: ((0.0 * CGFloat(M_PI)) / 360.0) * -1)
  78. self.view.layoutIfNeeded()
  79. })
  80. }
  81.  
  82. var viewAngle: CGFloat = 0 // Right-side up to start
  83. let π = CGFloat.pi // Swift allows special characters hold alt+p to use this, it allows for cleaner code
  84.  
  85. func rotate(by angle: CGFloat) {
  86.  
  87. for i in 0 ..< 4 {
  88. UIView.animate(withDuration: 0.125, delay: 0.125 * Double(i), options: .curveLinear, animations: {
  89.  
  90. self.viewAngle += angle/4
  91. self.rotateView.transform = CGAffineTransform(rotationAngle: self.viewAngle)
  92. self.view.layoutIfNeeded()
  93. }, completion: nil)
  94. }
  95.  
  96. }
  97.  
  98. rotate(by: π)
  99.  
  100. rotate(by: -π)
  101.  
  102. self.viewRoatate.transform = CGAffineTransform(rotationAngle: CGFloat.pi / -2)
  103. self.viewRoatate.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement