淺談iPhone開發常用代碼列舉

來源:互聯網
上載者:User

淺談iPhone開發常用代碼列舉是本文要介紹的內容,詳細的講述了iphone開發中一些常用的內容,不多說,先來看內容詳解吧。

UIEdgeInsets 設定包圍tableView的座標

 
  1. typedef struct UIEdgeInsets {  
  2.     CGFloat top, left, bottom, right;  // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'  
  3. } UIEdgeInsets; 

裡面分別是上,左,下,右的包圍長度,往下拖動時,如果top 》 0, 就會顯示出來,如果小於0就會隱藏。

計算字串的顯示長度

 
  1. CGSize detailSize = [@"你的字串" sizeWithFont:[UIFont systemFontOfSize:15] 
  2. constrainedToSize:CGSizeMake(200, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; 

navigationbar的back鍵觸發其他事件

 
  1. UIButton *back =[[UIButton alloc] initWithFrame:CGRectMake(200, 25, 63, 30)];  
  2. [back addTarget:self act   
  3. ion:@selector(reloadRowData:) forControlEvents:UIControlEventTouchUpInside];  
  4. [back setImage:[UIImage imageNamed:@"返回按鈕.png"] forState:UIControlStateNormal];  
  5. UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:back];  
  6. self.navigationItem.leftBarButtonItem = loginButtonItem 
  7. [back release];  
  8. [backButtonItem release]; 

防止螢幕暗掉鎖屏

 
  1. [[UIApplication sharedApplication] setIdleTimerDisabled:YES]; 

顯示網路活動狀態指示符

這是在iPhone左上部的狀態列顯示的轉動的表徵圖指示有背景發生網路的活動。

 
  1. UIApplication* app = [UIApplication sharedApplication];  
  2. app.networkActivityIndicatorVisible = YES; 

擷取UUID

 
  1. [[UIDevice currentDevice] uniqueIdentifier]  
  2.  
  3. UIDevice *myDevice = [UIDevice currentDevice];  
  4. NSString *deviceID = [myDevice uniqueIdentifier]; 

截取螢幕圖片

 
  1. UIGraphicsBeginImageContext(CGSizeMake(200,400));  //建立一個基於位元影像的圖形上下文並指定大小為CGSizeMake(200,400)  
  2. [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];  //renderInContext 呈現接受者及其子範圍到指定的上下文  
  3.  UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext(); //返回一個基於當前圖形內容相關的圖片  
  4. UIGraphicsEndImageContext();   //移除棧頂的基於當前位元影像的圖形上下文  
  5. imageData = UIImagePNGRepresentation(aImage); //以png格式返回指定圖片的資料 

應用程式邊框大小

我們應該使用"bounds"來獲得應用程式邊框。不是用"applicationFrame"。"applicationFrame"還包含了一個20像素的status bar。除非我們需要那額外的20像素的status bar。

震動和聲音播放

 
  1. Sound will work in the Simulator, however some sound (such as looped) has been 
  2. reported as not working in Simulator or even altogether depending on the audio format.
  3.  Note there are specific filetypes that must be used (.wav in this example). 
 
  1. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);  
  2. SystemSoundID pmph;  
  3. id sndpath = [NSBundle mainBundle] pathForResource:@"mySound"   ofType:@"wav"  inDirectory:@"/"];  
  4. CFURLRef baseURL = (CFURLRef) [NSURL alloc] initFileURLWithPath:sndpath];  
  5. AudioServicesCreateSystemSoundID (baseURL, &pmph);  
  6. AudioServicesPlaySystemSound(pmph);      
  7. [baseURL release]; 

Iphone擷取本機IP

 
  1. -(NSString *)getAddress {  
  2.     char iphone_ip[255];  
  3.     strcpy(iphone_ip,"127.0.0.1"); // if everything fails  
  4.     NSHost* myhost =[NSHost currentHost];  
  5.     if (myhost)  
  6.     {  
  7.         NSString *ad = [myhost address];  
  8.         if (ad)  
  9.             strcpy(iphone_ip,[ad cStringUsingEncoding:NSASCIIStringEncoding]);  
  10.     }  
  11.     return [NSString stringWithFormat:@"%s",iphone_ip];  

小結:淺談iPhone開發常用代碼列舉的內容介紹完了,希望本文對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.