- A notification is sent when the screen is switched. Just register a notification:
[Java]View Plaincopy
- [[Nsnotificationcenter Defaultcenter] Addobserver:self
- Selector:@selector (dorotateaction:)
- Name:uideviceorientationdidchangenotification
- Object:nil];
Then do the operation in the method:
[Java]View Plaincopy
- -(void) dorotateaction: (nsnotification *) notification{
- if ( [[uidevice currentdevice] orientation]==uiinterfaceorientationportrait
- | | [[uidevice currentdevice] orientation]==uiinterfaceorientationportraitupsidedown) {
- nslog (@
- }else{
- nslog (@
- }
- }
If you want to make a switch screen in the portal file, you can determine the orientation of the status bar:
[Java]View Plaincopy
- ////////////////////////////////////
- Notification delegate status bar has changed, into the screen operation
- -(void) Application: (UIApplication *) application didchangestatusbarorientation: (uiinterfaceorientation) oldstatusbarorientation{
- //clear background to prevent image residue from last screen turn
- [ImageView Setbackgroundcolor:[uicolor Clearcolor];
- //The following is a 4-direction switch between the horizontal and vertical screen, pay attention to turn the screen, regardless of which screen to turn. The starting coordinates are calculated from the starting point of the portrait direction (0,0) .
- if ([[[Uidevice Currentdevice] orientation]==uiinterfaceorientationportrait) {
- NSLog (@">>>portrait"); //home key in the next
- ImageView = [[Uiimageview alloc] Initwithframe:cgrectmake (0, 768, 44)];
- Imageview.backgroundcolor = [Uicolor Redcolor];
- [_window Addsubview:imageview];
- }Else if ([[[Uidevice Currentdevice] orientation]==uiinterfaceorientationportraitupsidedown) {
- NSLog (@">>>portraitupsidedown"); //home key on the
- ImageView = [[Uiimageview alloc] Initwithframe:cgrectmake (0, 960, 768, 44)];
- Imageview.backgroundcolor = [Uicolor Redcolor];
- [_window Addsubview:imageview];
- }Else if ([[[Uidevice Currentdevice] orientation]==uiinterfaceorientationlandscapeleft) {
- NSLog (@">>>landscapeleft"); //home key in left
- ImageView = [[Uiimageview alloc] Initwithframe:cgrectmake (0, 1024)];
- Imageview.backgroundcolor = [Uicolor Redcolor];
- [_window Addsubview:imageview];
- }
- Else if ([[Uidevice Currentdevice] orientation]==uiinterfaceorientationlandscaperight) {
- NSLog (@">>>landscaperight"); //home key on right
- ImageView = [[Uiimageview alloc] Initwithframe:cgrectmake (704, 0, 1024)];
- Imageview.backgroundcolor = [Uicolor Redcolor];
- [_window Addsubview:imageview];
- }
- }
iOS screen notifications