Detecting iOS Uidevice Orientation

Detecting iOS UIDevice orientation

Try doing the following when the application loads or when your view loads:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];

Then add the following method:

- (void) orientationChanged:(NSNotification *)note
{
UIDevice * device = note.object;
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
/* start special animation */
break;

case UIDeviceOrientationPortraitUpsideDown:
/* start special animation */
break;

default:
break;
};
}

The above will allow you to register for orientation changes of the device without enabling the autorotate of your view.



Note

In all cases in iOS, when you add an observor, also remove it at appropriate times (possibly, but not always, when the view appears/disappears). You can only have "pairs" of observe/unobserve code. If you do not do this the app will crash. Choosing where to observe/unobserve is beyond the scope of this QA. However you must have an "unobserve" to match the "observe" code above.

How to detect orientation change?

Here's how I got it working:

In AppDelegate.swift inside the didFinishLaunchingWithOptions function I put:

NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.rotated), name: UIDevice.orientationDidChangeNotification, object: nil)

and then inside the AppDelegate class I put the following function:

func rotated() {
if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) {
print("Landscape")
}

if UIDeviceOrientationIsPortrait(UIDevice.current.orientation) {
print("Portrait")
}
}

Hope this helps anyone else!

Thanks!

Swift - How to detect orientation changes


let const = "Background" //image name
let const2 = "GreyBackground" // image name
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()

imageView.image = UIImage(named: const)
// Do any additional setup after loading the view.
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if UIDevice.current.orientation.isLandscape {
print("Landscape")
imageView.image = UIImage(named: const2)
} else {
print("Portrait")
imageView.image = UIImage(named: const)
}
}

Detecting orientation on loading

You can check current orientation by UIApplication.shared.statusBarOrientation.isPortrait or UIApplication.shared.statusBarOrientation.isLandscape.

Detect iOS device orientation change without rotating view

I think you can subscribe to a notification

final class ViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, #selector(handleOrientationChange, name: UIDevice.orientationDidChangeNotification, object: nil))
}

deinit {
NotificationCenter.default.removeObserver(self)
}

@objc
private func handleOrientationChange() {
// Yay, orientation changed!
}
}

Detect device orientation in Init function

You are probably neglecting this documentation:

You also use the UIDevice instance to detect changes in the device’s
characteristics, such as physical orientation. You get the current
orientation using the orientation property or receive change
notifications by registering for the
UIDeviceOrientationDidChangeNotification notification. Before using
either of these techniques to get orientation data, you must enable
data delivery using the beginGeneratingDeviceOrientationNotifications
method
. When you no longer need to track the device orientation, call
the endGeneratingDeviceOrientationNotifications method to disable the
delivery of notifications.

Also, this is a 3-D orientation. You probably don't want that. What is the actual value of simply the orientation property? See:

enum UIDeviceOrientation : Int {
case Unknown
case Portrait
case PortraitUpsideDown
case LandscapeLeft
case LandscapeRight
case FaceUp
case FaceDown }

Probably what you really want is here:
"interfaceOrientation" is deprecated in iOS 8, How to change this method Objective C

Sometimes TraitCollections doesn't fill all your design needs. For
those cases, Apple recommends to compare view's bounds :

if view.bounds.size.width > view.bounds.size.height {
// ... }



Related Topics



Leave a reply



Submit