Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- // 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
- }
- 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()
- })
- }
- 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)
- }
- }
- rotate(by: π)
- rotate(by: -π)
- self.viewRoatate.transform = CGAffineTransform(rotationAngle: CGFloat.pi / -2)
- self.viewRoatate.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement