iOS開發實用技巧——屏幕適配研究


一、旋轉處理
    第一步:注冊通知 [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(changeFrames:)                                                     
                            name:UIDeviceOrientationDidChangeNotification
                                                       object:nil];
    第二把:處理接收事件
-(void)changeFrames:(NSNotification *)notification{
    NSLog(@"change notification: %@", notification.userInfo);
    float width=[[UIScreen mainScreen]bounds].size.width*[[UIScreen mainScreen] scale];
    float height=[[UIScreen mainScreen]bounds].size.height*[[UIScreen mainScreen] scale];
    if ([[UIDevice currentDevice] orientation]==UIInterfaceOrientationPortrait
        || [[UIDevice currentDevice] orientation]==UIInterfaceOrientationPortraitUpsideDown) {
        NSLog(@">>>portrait");
        self.frame=CGRectMake(0, 0, height, width);
    }else{
        NSLog(@">>>landscape");
        self.frame=CGRectMake(0, 0, width, height);
    }
    
    NSLog(@"view—> %@",self);
}

二、獲取屏幕分辨率
    //得到當前屏幕的尺寸:
    CGSize size_screen = [[UIScreenmainScreen]bounds].size;
    //獲得縮放率:
    CGFloat scale_screen = [UIScreen mainScreen].scale;   

    此時屏幕尺寸的寬高與scale的乘積就是相應的分辨率值。
    CGRect sizeOfA4 = CGRectMake(0, 0, 595, 842);//生成PDF文件時按A4標准
    CGRect sizeOfA5 = CGRectMake(0, 0, 421, 595);//生成PDF文件時按A5標准
    注意:不管scale=1還是scale=2,紙張的標准sizeOfA4和sizeOfA5的設置都不變,這是因為我們通常設置的寬高在iOS體系下都是邏輯上的point,並不是真實的像素!
只要屏幕是等比例放大縮小,[[UIScreenmainScreen]bounds].size都不變。不同scale的系統會自動放大或縮小,這就是所有的應用在320x480和640x960環境下無差別運行的原因。

三、設備標准
    iPhone/iPod Touch (320點 x 480點)
    普屏分辨率    320像素 x 480像素
    Retina分辨率 640像素 x 960像素

    iPad,iPad2/New iPad (768點 x 1024點)
    普屏        768像素 x 1024像素
    Retina屏  1536像素 x 2048像素

    換算關系 (在 iPhone3 上 1個 Point 相當於 1個pixel ; 而 iPhone4 上1個 point 就相當於4個 pixel;)
    普屏       1點 = 1像素   image.png
    Retina屏   1點 = 2像素   image@2x.png  


四、 真機與模擬器判斷+設備類型判斷
    #if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR
        NSLog(@" on simulator");
    #else
        NSLog(@"not on simulator");
    #endif
    注意:TARGET_OS_IPHONE在真機和模擬器上都是1
    設備類型判斷方法有兩種:
    1. UI_USER_INTERFACE_IDIOM() 進行區分(ios 3.2以上),但是無法區分iphone和ipod
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            //設備為ipad
        } else {
            //設備為iphone 或 ipod
        }
    2. 使用 UIDevice.model 進行區分  (ios 2.0 >=)
        NSString *deviceType = [UIDevice currentDevice].model;    
        if([deviceType isEqualToString:@"iPhone"]) {
              //iPhone
        }else if([deviceType isEqualToString:@"iPod touch"]) {
            //iPod Touch
        }else {
            //iPad
        }


五、獲取設備相關信息
    //軟件信息
    NSLog(@"sysname=%@",[[UIDevice currentDevice] systemName]);// 系統名
    NSLog(@"systemVersion=%@",[[UIDevice currentDevice] systemVersion]); //版本號
    NSLog(@"model=%@",[[UIDevice currentDevice] model]); //類型(ipad、ipod、iphone)而[[UIDevice currentDevice] userInterfaceIdiom]只能判斷iphone和ipad
    NSLog(@"olduuid=%@",[[UIDevice currentDevice] uniqueIdentifier]); //唯一識別碼 ios5.0開始deprecated
    NSLog(@"name=%@",[[UIDevice currentDevice] name]); //設備名稱
    NSLog(@"localizedModel=%@",[[UIDevice currentDevice] localizedModel]); // 本地模式
    NSLog(@"ios6UUID=%@",[[[UIDevice currentDevice] identifierForVendor] UUIDString]);//ios6.0開始available
   
   ----------注:以下內容未測試,摘自http://www.cnblogs.com/mrhgw/archive/2012/06/27/2565798.html---------------
    // 硬件信息
    [UIDevice platform];//平台
    [UIDevice cpuFrequency]];//cpu信息
    UIDevice busFrequency]];//總線
    [UIDevice totalMemory]];//總內存
    UIDevice userMemory]];//已經使用的內存
    -----------------------------------------------------------------------------------------------------------------------------
    //App信息
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    CFShow(infoDictionary);//所有plist內容
    // app名稱
    NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    // app build版本
    NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];

    判斷是否有照相機
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        NSLog(@"有");
    else
        NSLog(@"沒有");

六、針對不同分辨率的設備,程序員只需要做三件事:
    1.提供app高清圖標;
    2.UI圖片素材@2x.png;
    3.從網絡下載適配的圖片(判斷條件[[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2)
    -所有的iPhone、iPod Touch設備的 Point分辨率都是 320×480,也就是邏輯分辨率都一致,保證了App不需要修改也能正常的在高像素分辨率上運行,只是原來App中的圖片會被拉升后顯示,影響美觀,沒有發揮retina的優勢。
    -程序內所有的GCRectMake都是邏輯分辨率,不影響位置和大小!做游戲開發最有用,普通app影響不大
    -問題:如何進行相對布局???(6.0之前有autoResizeMask,autoresizing    6.0可使用與android相對布局類似的autoLayout)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM