The screen orientation.

Source: Internet
Author: User

Objective

The two days of learning about screen rotation related knowledge, but also extend the accelerator and gyroscope these previously did not go deep to learn the point of knowledge, without looking carefully before there are some questions, such as; the user turned off the screen rotation of the phone, but according to our experience, the app's interface can be rotated, For example, the video playback type of the app, or you can view the full screen, how is this done? And for example, your entire project does not allow horizontal screen display, and a single controller is required to display the horizontal screen, this is how to do? The user turned off the phone screen rotation, can we still determine the direction of the phone screen? With these questions, let's say one thing about the screen orientation.

From a simple start

first of all, the user allows the screen rotation of the mobile phone situation (clearly, your app allows rotation, the user does not allow the screen rotation that you can easily check the following is useless, as to how to turn, said back! )

If your entire project allows the screen, how to set, I think this everyone because it is clear, tick.

Next, if the user changes the direction of the phone screen, you need to do some of the corresponding actions, then you have to know whether the user is a horizontal screen or vertical screen, this time there is this notice Uideviceorientationdidchangenotification can play, every time the user changes the direction of the phone screen, We can all through this notice to determine the direction of the phone screen: (If the user has turned on the phone screen rotation function) If not opened, this notice I tested you the first time you open the app, this notification can be received, but because you lock the screen before the system will force the vertical screen, do not allow horizontal screen to turn off screen rotation! The first time you receive the notice will become meaningless, because all turned off the rotation, will not turn! How to receive the post-mail notice!

[[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (Orientchang:) Name: Uideviceorientationdidchangenotification Object:nil];

The above accepts the notice, the following can make the corresponding judgment:

-(void) Orientchang: (nsnotification *) odentifile{//Get current device instance uidevice *device = [Uidevice currentdevice];            Switch (device.orientation) {case Uideviceorientationfaceup:nslog (@ "The screen is lying flat");                    Break            Case Uideviceorientationfacedown:nslog (@ "The screen is lying flat");                    Break            Unable to determine the current direction of the device, it is possible to allow you to skew the case Uideviceorientationunknown:nslog (@ "Unknown direction");                    Break            Case Uideviceorientationlandscapeleft:nslog (@ "screen left horizontal");                    Break            Case Uideviceorientationlandscaperight:nslog (@ "screen right horizontal");                    Break            Case Uideviceorientationportrait:nslog (@ "screen upright");                    Break            Case Uideviceorientationportraitupsidedown:nslog (@ "screen upright, up and down upside down");                    Break            Default:nslog (@ "screen orientation is not recognized");    Break }}

Talk about the above note and the point of extension:

1th:

Uideviceorientationdidchangenotification notice, point in to see the words, see and it together have the following several notice, by the way also here to explain these several notice to everyone;

Uidevicebatterystatedidchangenotification battery status changes to notifications (like low battery mode below 20%)

Uidevicebatteryleveldidchangenotification Battery power Change notification

Uideviceproximitystatedidchangenotification This is a close-up sensor notification, like when you call the phone, when you close your face, the screen will be closed, you can turn off the phone will be bright, is the use of this, The following will also give links to the knowledge of this piece!

In fact, like uiapplicationdidchangestatusbarframenotification this status bar change notification can also judge the direction of the phone screen! In fact, the focus is not to use which, are the changes received after the screen notification, you receive notification after you know the screen direction changed immediately to determine the direction of the phone screen, this is the focus, is also the core bar!

Uikit_extern nsstring *const uideviceorientationdidchangenotification __tvos_prohibited; Uikit_extern nsstring *const uidevicebatterystatedidchangenotification   ns_available_ios (3_0) __TVOS_PROHIBITED ; Uikit_extern nsstring *const uidevicebatteryleveldidchangenotification   ns_available_ios (3_0) __TVOS_PROHIBITED ; Uikit_extern nsstring *const uideviceproximitystatedidchangenotification Ns_available_ios (3_0);

2nd:

Uidevice *device = [Uidevice Currentdevice] This is still necessary to extend the contents of the content, we have its common properties are also shown through the code;

NSString *zxname = [[Uidevice currentdevice] name]; NSLog (@ "Device name:%@", zxname); NSString *zxsysname = [[Uidevice currentdevice] systemName]; NSLog (@ "system name:%@", zxsysname);//Phone is iphone OS     nsstring *zxsysversion = [[Uidevice currentdevice] systemversion]; NSLog (@ "System version number:%@", zxsysversion);//Current is 9.3.4    nsstring *zxmodel = [[Uidevice currentdevice] model]; NSLog (@ "Device type:%@", zxmodel);//IPhone    nsstring *zxlocalizedmodel = [[Uidevice currentdevice] Localizedmodel]; NSLog (@ "Local device mode:%@", zxlocalizedmodel);//IPhone     /**  * uuidstring can be used to uniquely identify the device  */nsuuid * Identifierforvendor = [[Uidevice currentdevice] Identifierforvendor]; NSLog (@ "stridentifierforvendor:%@", identifierforvendor.uuidstring);

About this uuidstring to say two more sentences, also surf the internet to search this thing, the answer is this:

NSString *identifierforvendor = [[Uidevice Currentdevice].identifierforvendor uuidstring];

NSString *identifierforadvertising = [[Asidentifiermanager sharedmanager].advertisingidentifier UUIDString];

Explanatory notes :

Identifierforvendor is the only value for a vendor, which means that an app issued by the same company will have the same identifier when it runs on the same device. However, if the user removes the vendor's app and then re-installs it, the identifier will be inconsistent.

Advertisingidentifier will return the same value to all software vendors on the device, so it can only be used when advertising. This value can change in many cases, such as when the user initializes the device.

What if the user turns off the phone screen rotation?

This we also from the simple point of speaking, say a simple demand, the user turned off the phone screen rotation, we also have a player interface or need a horizontal screen display, what should we do? First of all, we have a full-screen click button, click on the button after the interface horizontal screen. Look at the code explanation, the button and click events are not written, directly to the point:

Uiinterfaceorientationlandscapeleft to the left, the specific right or left to the value of their own to take out. NSNumber *value = [NSNumber numberwithint:uiinterfaceorientationlandscapeleft]; [[Uidevice Currentdevice] setvalue:value forkey:@ "orientation"];

Click the button, this interface becomes the corresponding horizontal screen mode! In fact, there will be the corresponding inspiration to you, the whole app is not allowed to rotate, but this one interface to be able to cross-screen how to do? It's easy. This task will be given to you to practice, to feel no, You can leave a message or add me QQ contact me!

Coremotion.framework the protagonist is coming!

This frame is something that handles accelerators and gyroscopes! Add to your project first, import the system header file

#import <CoreMotion/CoreMotion.h>

iOS development----Cmdevicemotion Gyroscope using iOS learning note 34-accelerometer and gyroscope

First of all, my study link to everyone, thanks to the author!

As for what the gyroscope and accelerator are, I'm not going to describe it anymore. Principle we can see the above to the learning link! Tell me how to use them to judge the direction of your phone's screen! See Code Explanation:

-(void) zxmotionmanager{if (_motionmanager = = nil) {_motionmanager = [[Cmmotionmanager alloc] init]; } if ([_motionmanager isgyroavailable]) {//Devicemotion accelerator and gyroscope composite data _motionmanager.devicemotionupdate        Interval = 0.01f; [Self.motionmanager startdevicemotionupdatestoqueue:[nsoperationqueue Mainqueue] withHandler:^ (CMDeviceMotion * _ Nullable Motion, Nserror * _nullable error) {[Self Performselectoronmainthread: @selector (handledevicemotion:) wit       Hobject:motion Waituntildone:yes];      }];           } else {NSLog (@ "gyroscope/Accelerator Not available");     [Self setmotionmanager:nil];    }}-(void) Handledevicemotion: (cmdevicemotion *) devicemotion{double x = devicemotion.gravity.x;    Double y = devicemotion.gravity.y;        if (Fabs (y) >= fabs (x)) {if (y >= 0) {NSLog (@ "screen upright, up and down upside");        } else{NSLog (@ "screen upright"); }} else {if (x >= 0) {NSLog (@ "screen right horizontal");        } else{  
NSLog (@ "screen left horizontal"); } }}

You need to call the Zxmotionmanager method to determine the direction of the phone screen, even if the user turned off the phone rotation button!!

Expand:

The following content just feel fun, different view!

In the above learning link inside, there is such an effect, the picture seems to be with your mobile phone screen rotation and rotation, but carefully look like the direction is unchanged! Effects such as

First the accelerator and Gyro code to give out, but in fact, just any one of them do not have a good effect, will feel a flash flash!

-(void) creatmotionmanager{if (_motionmanager = = nil) {_motionmanager = [[Cmmotionmanager alloc] init]; } if ([_motionmanager isgyroavailable]) {__weak typeof (self) weakself = self;//Cmaccelerom Eterdata Accelerator effect//_motionmanager.accelerometerupdateinterval = 0.01f;//[Self.motionmanager startAccelerometerUp Datestoqueue:[nsoperationqueue Mainqueue] withhandler:^ (cmaccelerometerdata * _nullable AccelerometerData, NSError * _ Nullable error) {////Double rotation = atan2 (accelerometerdata.acceleration.x,accelerometerdata.accelera       TION.Y)-m_pi;//weakSelf.imageview.transform = cgaffinetransformmakerotation (rotation);                //      }];        Gyro effect _motionmanager.gyroupdateinterval = 0.01f; [Self.motionmanager startgyroupdatestoqueue:[nsoperationqueue Currentqueue] withhandler:^ (CMGyroData *gyroData, Nserror *error) {
Double rotation = atan2 (gyrodata.rotationrate.x, gyrodata.rotationrate.y)-m_pi; WeakSelf.imageview.transform = cgaffinetransformmakerotation (rotation); }]; }}

Finally verified, or they are two composite data effect comparison bar!

-(void) creatmotionmanager{       //devicemotion accelerator and Gyroscope composite data        _motionmanager.devicemotionupdateinterval = 0.01f ;        [Self.motionmanager startdevicemotionupdatestoqueue:[nsoperationqueue Mainqueue] withHandler:^ (CMDeviceMotion * _ Nullable Motion, Nserror * _nullable error) {                   double rotation = atan2 (motion.gravity.x,motion.gravity.y)-m_pi;        WeakSelf.imageview.transform = cgaffinetransformmakerotation (rotation);                    }];}

interested can try it yourself, to see what is the problem, add me q or message to me! !

The screen orientation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.