Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmcd/2854c9b7bd8e79ae4a02e7589ef097e2 to your computer and use it in GitHub Desktop.
Save jmcd/2854c9b7bd8e79ae4a02e7589ef097e2 to your computer and use it in GitHub Desktop.
Make individual UIViews appear rotated in an orientation locked app
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