淺談iPhone開發(fā)常用代碼列舉
淺談iPhone開發(fā)常用代碼列舉是本文要介紹的內(nèi)容,詳細(xì)的講述了iphone開發(fā)中一些常用的內(nèi)容,不多說,先來看內(nèi)容詳解吧。
UIEdgeInsets 設(shè)置包圍tableView的坐標(biāo)
- typedef struct UIEdgeInsets {
- CGFloat top, left, bottom, right; // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
- } UIEdgeInsets;
里面分別是上,左,下,右的包圍長度,往下拖動時,如果top 》 0, 就會顯示出來,如果小于0就會隱藏。
計算字符串的顯示長度
- CGSize detailSize = [@"你的字符串" sizeWithFont:[UIFont systemFontOfSize:15]
- constrainedToSize:CGSizeMake(200, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
navigationbar的back鍵觸發(fā)其他事件
- UIButton *back =[[UIButton alloc] initWithFrame:CGRectMake(200, 25, 63, 30)];
- [back addTarget:self act
- ion:@selector(reloadRowData:) forControlEvents:UIControlEventTouchUpInside];
- [back setImage:[UIImage imageNamed:@"返回按鈕.png"] forState:UIControlStateNormal];
- UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:back];
- self.navigationItem.leftBarButtonItem = loginButtonItem
- [back release];
- [backButtonItem release];
防止屏幕暗掉鎖屏
- [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
顯示網(wǎng)絡(luò)活動狀態(tài)指示符
這是在iPhone左上部的狀態(tài)欄顯示的轉(zhuǎn)動的圖標(biāo)指示有背景發(fā)生網(wǎng)絡(luò)的活動。
- UIApplication* app = [UIApplication sharedApplication];
- app.networkActivityIndicatorVisible = YES;
獲取UUID
- [[UIDevice currentDevice] uniqueIdentifier]
- UIDevice *myDevice = [UIDevice currentDevice];
- NSString *deviceID = [myDevice uniqueIdentifier];
截取屏幕圖片
- UIGraphicsBeginImageContext(CGSizeMake(200,400)); //創(chuàng)建一個基于位圖的圖形上下文并指定大小為CGSizeMake(200,400)
- [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; //renderInContext 呈現(xiàn)接受者及其子范圍到指定的上下文
- UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext(); //返回一個基于當(dāng)前圖形上下文的圖片
- UIGraphicsEndImageContext(); //移除棧頂?shù)幕诋?dāng)前位圖的圖形上下文
- imageData = UIImagePNGRepresentation(aImage); //以png格式返回指定圖片的數(shù)據(jù)
應(yīng)用程序邊框大小
我們應(yīng)該使用"bounds"來獲得應(yīng)用程序邊框。不是用"applicationFrame"。"applicationFrame"還包含了一個20像素的status bar。除非我們需要那額外的20像素的status bar。
震動和聲音播放
- Sound will work in the Simulator, however some sound (such as looped) has been
- reported as not working in Simulator or even altogether depending on the audio format.
- Note there are specific filetypes that must be used (.wav in this example).
- AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
- SystemSoundID pmph;
- id sndpath = [NSBundle mainBundle] pathForResource:@"mySound" ofType:@"wav" inDirectory:@"/"];
- CFURLRef baseURL = (CFURLRef) [NSURL alloc] initFileURLWithPath:sndpath];
- AudioServicesCreateSystemSoundID (baseURL, &pmph);
- AudioServicesPlaySystemSound(pmph);
- [baseURL release];
Iphone獲取本機IP
- -(NSString *)getAddress {
- char iphone_ip[255];
- strcpy(iphone_ip,"127.0.0.1"); // if everything fails
- NSHost* myhost =[NSHost currentHost];
- if (myhost)
- {
- NSString *ad = [myhost address];
- if (ad)
- strcpy(iphone_ip,[ad cStringUsingEncoding:NSASCIIStringEncoding]);
- }
- return [NSString stringWithFormat:@"%s",iphone_ip];
- }
小結(jié):淺談iPhone開發(fā)常用代碼列舉的內(nèi)容介紹完了,希望本文對你有所幫助!