淺談iPhone開發常用代碼列舉
淺談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開發常用代碼列舉的內容介紹完了,希望本文對你有所幫助!