Last active
August 1, 2019 17:12
-
-
Save jmcd/2854c9b7bd8e79ae4a02e7589ef097e2 to your computer and use it in GitHub Desktop.
Make individual UIViews appear rotated in an orientation locked app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var imageView: UIImageView! | |
@IBOutlet weak var label0: UILabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
NotificationCenter.default.addObserver(self, selector: #selector(rotated), name: UIDevice.orientationDidChangeNotification, object: nil) | |
} | |
@objc | |
func rotated() { | |
let at: CGAffineTransform | |
switch(UIDevice.current.orientation){ | |
case .landscapeLeft: | |
at = CGAffineTransform.init(rotationAngle: CGFloat.pi/2) | |
case .landscapeRight: | |
at = CGAffineTransform.init(rotationAngle: -CGFloat.pi/2) | |
case .portraitUpsideDown: | |
at = CGAffineTransform.init(rotationAngle: CGFloat.pi) | |
default: | |
at = CGAffineTransform.identity | |
} | |
UIView.animate(withDuration: 0.5) { | |
self.imageView.layer.setAffineTransform(at) | |
self.label0.layer.setAffineTransform(at) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment