一、旋轉(zhuǎn)處理
第一步:注冊(cè)通知 [[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);
}
二、獲取屏幕分辨率
//得到當(dāng)前屏幕的尺寸:
CGSize size_screen = [[UIScreenmainScreen]bounds].size;
//獲得縮放率:
CGFloat scale_screen = [UIScreen mainScreen].scale;
此時(shí)屏幕尺寸的寬高與scale的乘積就是相應(yīng)的分辨率值。
CGRect sizeOfA4 = CGRectMake(0, 0, 595, 842);//生成PDF文件時(shí)按A4標(biāo)準(zhǔn)
CGRect sizeOfA5 = CGRectMake(0, 0, 421, 595);//生成PDF文件時(shí)按A5標(biāo)準(zhǔn)
注意:不管scale=1還是scale=2,紙張的標(biāo)準(zhǔn)sizeOfA4和sizeOfA5的設(shè)置都不變,這是因?yàn)槲覀兺ǔTO(shè)置的寬高在iOS體系下都是邏輯上的point,并不是真實(shí)的像素!
只要屏幕是等比例放大縮小,[[UIScreenmainScreen]bounds].size都不變。不同scale的系統(tǒng)會(huì)自動(dòng)放大或縮小,這就是所有的應(yīng)用在320x480和640x960環(huán)境下無(wú)差別運(yùn)行的原因。
三、設(shè)備標(biāo)準(zhǔn)
iPhone/iPod Touch (320點(diǎn) x 480點(diǎn))
普屏分辨率 320像素 x 480像素
Retina分辨率 640像素 x 960像素
iPad,iPad2/New iPad (768點(diǎn) x 1024點(diǎn))
普屏 768像素 x 1024像素
Retina屏 1536像素 x 2048像素
換算關(guān)系 (在 iPhone3 上 1個(gè) Point 相當(dāng)于 1個(gè)pixel ; 而 iPhone4 上1個(gè) point 就相當(dāng)于4個(gè) pixel;)
普屏 1點(diǎn) = 1像素 image.png
Retina屏 1點(diǎn) = 2像素 image@2x.png
四、 真機(jī)與模擬器判斷+設(shè)備類型判斷
#if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR
NSLog(@" on simulator");
#else
NSLog(@"not on simulator");
#endif
注意:TARGET_OS_IPHONE在真機(jī)和模擬器上都是1
設(shè)備類型判斷方法有兩種:
1. UI_USER_INTERFACE_IDIOM() 進(jìn)行區(qū)分(ios 3.2以上),但是無(wú)法區(qū)分iphone和ipod
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//設(shè)備為ipad
} else {
//設(shè)備為iphone 或 ipod
}
2. 使用 UIDevice.model 進(jìn)行區(qū)分 (ios 2.0 >=)
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:@"iPhone"]) {
//iPhone
}else if([deviceType isEqualToString:@"iPod touch"]) {
//iPod Touch
}else {
//iPad
}
五、獲取設(shè)備相關(guān)信息
//軟件信息
NSLog(@"sysname=%@",[[UIDevice currentDevice] systemName]);// 系統(tǒng)名
NSLog(@"systemVersion=%@",[[UIDevice currentDevice] systemVersion]); //版本號(hào)
NSLog(@"model=%@",[[UIDevice currentDevice] model]); //類型(ipad、ipod、iphone)而[[UIDevice currentDevice] userInterfaceIdiom]只能判斷iphone和ipad
NSLog(@"olduuid=%@",[[UIDevice currentDevice] uniqueIdentifier]); //唯一識(shí)別碼 ios5.0開(kāi)始deprecated
NSLog(@"name=%@",[[UIDevice currentDevice] name]); //設(shè)備名稱
NSLog(@"localizedModel=%@",[[UIDevice currentDevice] localizedModel]); // 本地模式
NSLog(@"ios6UUID=%@",[[[UIDevice currentDevice] identifierForVendor] UUIDString]);//ios6.0開(kāi)始available
注:以下內(nèi)容未測(cè)試,
// 硬件信息
[UIDevice platform];//平臺(tái)
[UIDevice cpuFrequency]];//cpu信息
UIDevice busFrequency]];//總線
[UIDevice totalMemory]];//總內(nèi)存
UIDevice userMemory]];//已經(jīng)使用的內(nèi)存
-----------------------------------------------------------------------------------------------------------------------------
//App信息
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
CFShow(infoDictionary);//所有plist內(nèi)容
// app名稱
NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
// app版本
NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
// app build版本
NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
判斷是否有照相機(jī)
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
NSLog(@"有");
else
NSLog(@"沒(méi)有");
六、針對(duì)不同分辨率的設(shè)備,程序員只需要做三件事:
1.提供app高清圖標(biāo);
2.UI圖片素材@2x.png;
3.從網(wǎng)絡(luò)下載適配的圖片(判斷條件[[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2)
-所有的iPhone、iPod Touch設(shè)備的 Point分辨率都是 320×480,也就是邏輯分辨率都一致,保證了App不需要修改也能正常的在高像素分辨率上運(yùn)行,只是原來(lái)App中的圖片會(huì)被拉升后顯示,影響美觀,沒(méi)有發(fā)揮retina的優(yōu)勢(shì)。
-程序內(nèi)所有的GCRectMake都是邏輯分辨率,不影響位置和大?。∽鲇螒蜷_(kāi)發(fā)最有用,普通app影響不大
-問(wèn)題:如何進(jìn)行相對(duì)布局???(6.0之前有autoResizeMask,autoresizing 6.0可使用與android相對(duì)布局類似的autoLayout)
聯(lián)系客服