淺談iPhone開發常用代碼列舉是本文要介紹的內容,詳細的講述了iphone開發中一些常用的內容,不多說,先來看內容詳解吧。
UIEdgeInsets 設定包圍tableView的座標
- 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鍵觸發其他事件
- 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];
顯示網路活動狀態指示符
這是在iPhone左上部的狀態列顯示的轉動的表徵圖指示有背景發生網路的活動。
- UIApplication* app = [UIApplication sharedApplication];
- app.networkActivityIndicatorVisible = YES;
擷取UUID
- [[UIDevice currentDevice] uniqueIdentifier]
-
- UIDevice *myDevice = [UIDevice currentDevice];
- NSString *deviceID = [myDevice uniqueIdentifier];
截取螢幕圖片
- UIGraphicsBeginImageContext(CGSizeMake(200,400)); //建立一個基於位元影像的圖形上下文並指定大小為CGSizeMake(200,400)
- [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; //renderInContext 呈現接受者及其子範圍到指定的上下文
- UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext(); //返回一個基於當前圖形內容相關的圖片
- UIGraphicsEndImageContext(); //移除棧頂的基於當前位元影像的圖形上下文
- imageData = UIImagePNGRepresentation(aImage); //以png格式返回指定圖片的資料
應用程式邊框大小
我們應該使用"bounds"來獲得應用程式邊框。不是用"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];
- }
小結:淺談iPhone開發常用代碼列舉的內容介紹完了,希望本文對你有所協助!