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 ----------注:以下内容未测试--------------- // 硬件信息 [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)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读