成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

關于iPhone SDK示例代碼解析

移動開發 iOS
本文介紹的是關于iPhone SDK示例代碼解析,主要是對iphone sdk一些常用的代碼進行來詳解,先來看詳細內容。

關于iPhone SDK示例代碼解析是本文要介紹的內容,主要是對iphone sdk一些常用的代碼進行來詳解,來看詳細內容講解。

在Xcode里,點菜單Run > Console 就可以看到NSLog的記錄.

  1. NSLog(@"log: %@ ", myString);   
  2. NSLog(@"log: %f ", myFloat);   
  3. NSLog(@"log: %i ", myInt); 

圖片顯示

不需要UI資源綁定,在屏幕任意處顯示圖片。 下面的代碼可以被用到任意 View 里面。

  1. CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);   
  2. UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];   
  3. [myImage setImage:[UIImage imageNamed:@"myImage.png"]];   
  4. myImage.opaque = YES; // explicitly opaque for performance   
  5. [self.view addSubview:myImage];   
  6. [myImage release]; 

應用程序邊框大小

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

Web view

UIWebView類的調用.

  1. CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);   
  2. UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];   
  3. [webView setBackgroundColor:[UIColor whiteColor]];   
  4. NSString *urlAddress = @"http://www.google.com";   
  5. NSURL *url = [NSURL URLWithString:urlAddress];   
  6. NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];   
  7. [webView loadRequest:requestObj];   
  8. [self addSubview:webView];   
  9. [webView release]; 

顯示網絡激活狀態圖標

在iPhone的狀態欄的左上方顯示的一個icon假如在旋轉的話,那就說明現在網絡正在被使用。

  1. UIApplication* app = [UIApplication sharedApplication];   
  2. app.networkActivityIndicatorVisible = YES; // to stop it, set this to NO 

Animation: 一組圖片

連續的顯示一組圖片

  1. NSArray *myImages = [NSArray arrayWithObjects:   
  2.     [UIImage imageNamed:@"myImage1.png"],   
  3.     [UIImage imageNamed:@"myImage2.png"],   
  4.     [UIImage imageNamed:@"myImage3.png"],   
  5.     [UIImage imageNamed:@"myImage4.gif"],   
  6.     nil];   
  7. UIImageView *myAnimatedView = [UIImageView alloc];   
  8. [myAnimatedView initWithFrame:[self bounds]];   
  9. myAnimatedView.animationImages = myImages;   
  10. myAnimatedView.animationDuration = 0.25; // seconds   
  11. myAnimatedView.animationRepeatCount = 0; // 0 = loops forever   
  12. [myAnimatedView startAnimating];   
  13. [self addSubview:myAnimatedView];   
  14. [myAnimatedView release]; 

Animation: 移動一個對象

讓一個對象在屏幕上顯示成一個移動軌跡。注意:這個Animation叫"fire and forget"。也就是說編程人員不能夠在animation過程中獲得任何信息(比如當前的位置)。假如你需要這個信息的話,那么就需要通過animate和定時器在必要的時候去調整x&y坐標。

  1. CABasicAnimation *theAnimation;       
  2. theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];   
  3. theAnimation.duration=1;   
  4. theAnimation.repeatCount=2;   
  5. theAnimation.autoreverses=YES;   
  6. theAnimation.fromValue=[NSNumber numberWithFloat:0];   
  7. theAnimation.toValue=[NSNumber numberWithFloat:-60];   
  8. [view.layer addAnimation:theAnimation forKey:@"animateLayer"]; 

NSString和int類型轉換

下面的這個例子讓一個text label顯示的一個整型的值。

  1. currentScoreLabel.text = [NSString stringWithFormat:@"%d", currentScore]; 

正澤表達式 (RegEx)

當前的framework還不支持RegEx。開發人員還不能在iPhone上使用包括NSPredicate在類的regex。但是在模擬器上是可以使用NSPredicate的,就是不能在真機上支持。

可以拖動的對象items

下面展示如何簡單的創建一個可以拖動的image對象:

1、創建一個新的類來繼承UIImageView。

  1. @interface myDraggableImage : UIImageView {   

2、在新的類實現的時候添加兩個方法:

  1. - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {   
  2. // Retrieve the touch point   
  3.     CGPoint pt = [[touches anyObject] locationInView:self];   
  4.     startLocation = pt;   
  5.     [[self superview] bringSubviewToFront:self];   
  6. }   
  7. - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {   
  8. // Move relative to the original touch point   
  9.     CGPoint pt = [[touches anyObject] locationInView:self];   
  10.     CGRect frame = [self frame];   
  11.     frame.origin.x += pt.x – startLocation.x;   
  12.     frame.origin.y += pt.y – startLocation.y;   
  13.     [self setFrame:frame];   

3、現在再創建一個新的image加到我們剛創建的UIImageView里面,就可以展示了。

  1. dragger = [[myDraggableImage alloc] initWithFrame:myDragRect];   
  2. [dragger setImage:[UIImage imageNamed:@"myImage.png"]];   
  3. [dragger setUserInteractionEnabled:YES]; 

震動和聲音播放

下面介紹的就是如何讓手機震動(注意:在simulator里面不支持震動,但是他可以在真機上支持。)

  1. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 

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).

  1. SystemSoundID pmph;   
  2. id sndpath = [[NSBundle mainBundle]   
  3.     pathForResource:@"mySound"   
  4.     ofType:@"wav"   
  5.     inDirectory:@"/"];   
  6. CFURLRef baseURL = (CFURLRef) [[NSURL alloc] initFileURLWithPath:sndpath];   
  7. AudioServicesCreateSystemSoundID (baseURL, &pmph);   
  8. AudioServicesPlaySystemSound(pmph);       
  9. [baseURL release]; 

線程

1、創建一個新的線程:

  1. [NSThread detachNewThreadSelector:@selector(myMethod)   
  2.         toTarget:self   
  3.         withObject:nil]; 

2、創建線程所調用的方法:

  1. - (void)myMethod {   
  2.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];   
  3. *** code that should be run in the new thread goes here ***   
  4.     [pool release];   

假如我們需要在線程里面調用主線程的方法函數,就可以用performSelectorOnMainThread來實現:

  1. [self performSelectorOnMainThread:@selector(myMethod)   
  2.     withObject:nil   
  3.     waitUntilDone:false]; 

讀取crash的日記文件

假如很不幸,我們的某處代碼引起了crash,那么就可以閱讀這篇文章應該會有用: navigate here

如何進行測試

1、在模擬器里,點擊 Hardware > Simulate Memory Warning to test. 那么我們整個程序每個頁面就都能夠支持這個功能了。

2、Be sure to test your app in Airplane Mode.

  1. Access properties/methods in other classes  
  2.  
  3. One way to do this is via the AppDelegate:  
  4.  
  5. myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];  
  6.  
  7.  [[[appDelegate rootViewController] flipsideViewController] myMethod]; 

創建隨機數

調用arc4random()來創建隨機數. 還可以通過random()來創建, 但是必須要手動的設置seed跟系統時鐘綁定。這樣才能夠確保每次得到的值不一樣。所以相比較而言arc4random()更好一點。

定時器

下面的這個定時器會每分鐘調用一次調用myMethod。

  1. [NSTimer scheduledTimerWithTimeInterval:1   
  2.     target:self   
  3.     selector:@selector(myMethod)   
  4.     userInfo:nil   
  5.     repeats:YES]; 

當我們需要給定時器的處理函數myMethod傳參數的時候怎么辦?用"userInfo"屬性。

1、首先創建一個定時器:

  1. [NSTimer scheduledTimerWithTimeInterval:1   
  2.     target:self   
  3.     selector:@selector(myMethod)   
  4.     userInfo:myObject   
  5.     repeats:YES]; 

2、然后傳遞NSTimer對象到處理函數:

  1. -(void)myMethod:(NSTimer*)timer {   
  2. // Now I can access all the properties and methods of myObject   
  3.     [[timer userInfo] myObjectMethod];   

用"invalidate"來停止定時器:

  1. [myTimer invalidate];   
  2. myTimer = nil; // ensures we never invalidate an already invalid Timer 

應用分析

當應用程序發布版本的時候,我們可能會需要收集一些數據,比如說程序被使用的頻率如何。這個時候大多數的人使用PinchMedia來實現。他們會提供我們可以很方便的加到程序里面的Obj-C代碼,然后就可以通過他們的網站來查看統計數據。

Time

  1. Calculate the passage of time by using CFAbsoluteTimeGetCurrent().  
  2.  
  3. CFAbsoluteTime myCurrentTime = CFAbsoluteTimeGetCurrent(); // perform calculations here 

警告窗口

顯示一個簡單的帶OK按鈕的警告窗口。

  1. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"An Alert!"   
  2.         delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];   
  3. [alert show];   
  4. [alert release]; 

Plist文件

應用程序特定的plist文件可以被保存到app bundle的Resources文件夾。當應用程序運行起來的時候,就會去檢查是不是有一個plist文件在用戶的Documents文件夾下。假如沒有的話,就會從app bundle目錄下拷貝過來。

  1. // Look in Documents for an existing plist file   
  2. NSArray *paths = NSSearchPathForDirectoriesInDomains(   
  3.     NSDocumentDirectory, NSUserDomainMask, YES);   
  4. NSString *documentsDirectory = [paths objectAtIndex:0];   
  5. myPlistPath = [documentsDirectory stringByAppendingPathComponent:   
  6.     [NSString stringWithFormat: @"%@.plist", plistName] ];   
  7. [myPlistPath retain];   
  8. // If it’s not there, copy it from the bundle   
  9. NSFileManager *fileManger = [NSFileManager defaultManager];   
  10. if ( ![fileManger fileExistsAtPath:myPlistPath] ) {   
  11.     NSString *pathToSettingsInBundle = [[NSBundle mainBundle]   
  12.         pathForResource:plistName ofType:@"plist"];   
  13. }        

現在我們就可以從Documents文件夾去讀plist文件了。

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(   
  2.     NSDocumentDirectory, NSUserDomainMask, YES);   
  3. NSString *documentsDirectoryPath = [paths objectAtIndex:0];   
  4. NSString *path = [documentsDirectoryPath   
  5.     stringByAppendingPathComponent:@"myApp.plist"];   
  6. NSMutableDictionary *plist = [NSDictionary dictionaryWithContentsOfFile: path]; 

Now read and set key/values

  1. myKey = (int)[[plist valueForKey:@"myKey"] intValue];   
  2. myKey2 = (bool)[[plist valueForKey:@"myKey2"] boolValue];   
  3. [plist setValue:myKey forKey:@"myKey"];   
  4. [plist writeToFile:path atomically:YES]; 

Info button

為了更方便End-User去按,我們可以增大Info button上可以觸摸的區域。

  1. CGRect newInfoButtonRect = CGRectMake(infoButton.frame.origin.x-25,   
  2.     infoButton.frame.origin.y-25, infoButton.frame.size.width+50,   
  3.     infoButton.frame.size.height+50);   
  4. [infoButton setFrame:newInfoButtonRect]; 

查找Subviews(Detecting Subviews)

我們可以通過循環來查找一個已經存在的View。當我們使用view的tag屬性的話,就很方便實現Detect Subviews。

  1. for (UIImageView *anImage in [self.view subviews]) {   
  2. if (anImage.tag == 1) {   
  3. // do something   
  4.     }   

手冊文檔

  1. Official Apple How-To’s  
  2. Learn Objective-C 

小結:關于iPhone SDK示例代碼解析的內容介紹完了,希望本文對你有所幫助!

責任編輯:zhaolei 來源: 網絡轉載
相關推薦

2011-08-18 10:06:10

2011-07-06 17:40:43

iPhone SDK

2011-07-06 17:53:40

iPhone SDK Xcode

2011-08-09 14:54:29

iPhoneNSDateanotherDate

2011-08-12 13:19:24

iPhoneSDK安裝

2009-12-07 15:41:51

PHP圖片加水印

2010-02-24 13:38:18

WCF PreCal模

2009-12-18 16:00:29

Ruby獲取當前類名

2010-02-22 15:06:31

WCF信道監聽器

2011-08-01 15:17:17

iPhone開發 證書 簽名

2011-08-19 10:05:30

iPhone開發

2011-07-18 09:35:29

iPhone 框架

2010-01-14 13:08:37

VB.NET運算符

2010-03-05 15:01:29

Python解析XML

2011-06-02 17:27:49

iphone 多線程

2011-08-11 11:37:34

iPhone內存

2011-07-18 14:39:53

iPhone SDK UIKit

2009-12-02 10:49:59

PHP解析XML元素結

2011-08-12 11:22:53

Oracle存儲過程Java

2021-08-04 14:32:16

鴻蒙HarmonyOS應用
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 狠狠狠色丁香婷婷综合久久五月 | www.国产一区| 国产jizz女人多喷水99 | 亚洲成人福利在线观看 | 日韩成人在线观看 | 一级欧美日韩 | 国产片侵犯亲女视频播放 | 国产精品久久久久无码av | 亚洲福利av | 亚洲天堂999 | 夜夜骑首页 | 亚洲精品日韩一区二区电影 | 亚洲色图在线观看 | 国产精品99久久久久久www | 国内精品视频在线观看 | 久久国产亚洲 | 国产精品99久久久久久久久 | 天天草天天爱 | 国产99久久久久 | 欧美激情视频网站 | 免费三级av| 中文字幕欧美一区 | 色婷婷一区二区三区四区 | 成人免费视频播放 | 黑人巨大精品欧美一区二区免费 | 欧美成年黄网站色视频 | 成人国产免费视频 | 国产激情片在线观看 | 国产精品一区二区三区在线播放 | 91欧美激情一区二区三区成人 | 久久久久久久香蕉 | 九色91视频 | 国产成人精品一区二 | 国产亚洲精品精品国产亚洲综合 | 欧美精品一二区 | 久久精品亚洲欧美日韩精品中文字幕 | 精品视频网 | www.亚洲一区 | 国产精品久久久久久福利一牛影视 | 中国av在线免费观看 | 成av在线|