2014年2月17日 星期一

iOS 7上portrait landscape要如何在不同分頁上,做不同的設定。

為了這個螢幕轉方向的問題,弄了好久才找到最佳解法,之前用了一個比較蠢的解法,就是設定一個事件去監聽轉換的方向,然後開始針對不同版面進行為整形....一個版面刻了兩個小時,就是為了橫向直向都要可以,一整個累.....如果有需求的,可以參考一下這個方法,雖然我覺得沒有很好,請注意這邊的做法確定在ios6、ios7上確實可行,但在ios5以下版本,我就不敢確定了,我就先來說明這個方法。

Step1:宣告一個監聽事件,監聽UIDeviceOrientationDidChangeNotification方向轉換


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

Step2:撰寫function orientationChanged,針對不同規格刻版面。

- (void)orientationChanged:(NSNotification *)notification
{
    CGFloat img_x=0.0f,img_y=65.0f,img_width=320.0f,img_height=40.0f;
    CGFloat Seg_x=10.0f,Seg_y=70.0f,Seg_width=300.0f,Seg_height=30.0f;
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ){
         NSLog(@"ipad");
        if (UIDeviceOrientationIsLandscape(deviceOrientation))
        {
            //do something is Landscape
            img_x=0.0f,img_y=65.0f,img_width=1024.0f,img_height=50.0f;
            Seg_x=253.0f,Seg_y=75.0f,Seg_width=500.0f,Seg_height=30.0f;
            if (debug) NSLog(@"UIDeviceOrientationIsLandscape");
            imageView.frame=CGRectMake(img_x,img_y,img_width,img_height);
            mySegmentedControl.frame=CGRectMake(Seg_x,Seg_y,Seg_width,Seg_height);
        }
        else if (UIDeviceOrientationIsPortrait(deviceOrientation))
        {
            //do something is Portrait
            img_x=0.0f,img_y=65.0f,img_width=768.0f,img_height=50.0f;
            Seg_x=130.0f,Seg_y=75.0f,Seg_width=500.0f,Seg_height=30.0f;
            if (debug) NSLog(@"UIDeviceOrientationIsPortrait");
            imageView.frame=CGRectMake(img_x,img_y,img_width,img_height);
            mySegmentedControl.frame=CGRectMake(Seg_x,Seg_y,Seg_width,Seg_height);
        }
    }else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
        NSLog(@"iphone");
        if (UIDeviceOrientationIsLandscape(deviceOrientation))
        {
            //do something is Landscape
            img_x=0.0f,img_y=53.5f,img_width=568.0f,img_height=40.0f;
            Seg_x=138.0f,Seg_y=60.0f,Seg_width=300.0f,Seg_height=30.0f;
            if (debug) NSLog(@"UIDeviceOrientationIsLandscape");
            imageView.frame=CGRectMake(img_x,img_y,img_width,img_height);
            mySegmentedControl.frame=CGRectMake(Seg_x,Seg_y,Seg_width,Seg_height);
        }
        else if (UIDeviceOrientationIsPortrait(deviceOrientation))
        {
            //do something is Portrait
            img_x=0.0f,img_y=65.5f,img_width=320.0f,img_height=40.0f;
            Seg_x=10.0f,Seg_y=70.0f,Seg_width=300.0f,Seg_height=30.0f;
            if (debug) NSLog(@"UIDeviceOrientationIsPortrait");
            if (deviceOrientation==UIDeviceOrientationPortraitUpsideDown){
                img_x=0.0f,img_y=53.5f,img_width=568.0f,img_height=40.0f;
                Seg_x=138.0f,Seg_y=60.0f,Seg_width=300.0f,Seg_height=30.0f;
            }
            imageView.frame=CGRectMake(img_x,img_y,img_width,img_height);
            mySegmentedControl.frame=CGRectMake(Seg_x,Seg_y,Seg_width,Seg_height);
        }
    }
    
}

另外一種做法,可以說是比較懶惰的做法,只要刻一個版面就好,可以針對不同需求來作設定。
我想大家都應該知道ios6以上針對版面的轉換,已經更新了函式,現在是

- (BOOL)shouldAutorotate
{
    //returns true if want to allow orientation change
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
    //decide number of origination tob supported by Viewcontroller.
    return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    //from here you Should try to Preferred orientation for ViewController
    return UIInterfaceOrientationPortrait;
}
但是呢?如果你用stroyboard來做版面的時候,會發現,似乎沒辦法針對每個版面進行不同的設定,例如你要再navigation controller 將 portrait 轉成 landscape,這個意思是第一個版面是portrait,第二個頁面是landscape,這樣就會遇到了困難,這樣怎麼解決呢?

Step1:在AppDelegate內的@end後加入下面程式碼


@implementation UINavigationController (Rotation_IOS6)

-(BOOL)shouldAutorotate
{
    //NSLog(@"shouldAutorotate");
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    //NSLog(@"supportedInterfaceOrientations");
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    //NSLog(@"preferredInterfaceOrientationForPresentation");
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end

Step2:在你要的頁面中加入上述的函式即可

- (BOOL)shouldAutorotate
{
    //returns true if want to allow orientation change
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
    //decide number of origination tob supported by Viewcontroller.
    return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    //from here you Should try to Preferred orientation for ViewController
    return UIInterfaceOrientationPortrait;
}