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

關(guān)于iPhone SDK示例代碼解析

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

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

在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]; 

應(yīng)用程序邊框大小

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

Web view

UIWebView類的調(diào)用.

  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]; 

顯示網(wǎng)絡(luò)激活狀態(tài)圖標

在iPhone的狀態(tài)欄的左上方顯示的一個icon假如在旋轉(zhuǎn)的話,那就說明現(xiàn)在網(wǎng)絡(luò)正在被使用。

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

Animation: 一組圖片

連續(xù)的顯示一組圖片

  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和定時器在必要的時候去調(diào)整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類型轉(zhuǎn)換

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

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

正澤表達式 (RegEx)

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

可以拖動的對象items

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

1、創(chuàng)建一個新的類來繼承UIImageView。

  1. @interface myDraggableImage : UIImageView {   

2、在新的類實現(xiàn)的時候添加兩個方法:

  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、現(xiàn)在再創(chuàng)建一個新的image加到我們剛創(chuàng)建的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、創(chuàng)建一個新的線程:

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

2、創(chuàng)建線程所調(diào)用的方法:

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

假如我們需要在線程里面調(diào)用主線程的方法函數(shù),就可以用performSelectorOnMainThread來實現(xiàn):

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

讀取crash的日記文件

假如很不幸,我們的某處代碼引起了crash,那么就可以閱讀這篇文章應(yīng)該會有用: 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]; 

創(chuàng)建隨機數(shù)

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

定時器

下面的這個定時器會每分鐘調(diào)用一次調(diào)用myMethod。

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

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

1、首先創(chuàng)建一個定時器:

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

2、然后傳遞NSTimer對象到處理函數(shù):

  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 

應(yīng)用分析

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

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文件

應(yīng)用程序特定的plist文件可以被保存到app bundle的Resources文件夾。當應(yīng)用程序運行起來的時候,就會去檢查是不是有一個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. }        

現(xiàn)在我們就可以從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上可以觸摸的區(qū)域。

  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)

我們可以通過循環(huán)來查找一個已經(jīng)存在的View。當我們使用view的tag屬性的話,就很方便實現(xiàn)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 

小結(jié):關(guān)于iPhone SDK示例代碼解析的內(nèi)容介紹完了,希望本文對你有所幫助!

責任編輯:zhaolei 來源: 網(wǎng)絡(luò)轉(zhuǎn)載
相關(guān)推薦

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-12 13:19:24

iPhoneSDK安裝

2011-08-09 14:54:29

iPhoneNSDateanotherDate

2010-02-24 13:38:18

WCF PreCal模

2009-12-07 15:41:51

PHP圖片加水印

2010-02-22 15:06:31

WCF信道監(jiān)聽器

2009-12-18 16:00:29

Ruby獲取當前類名

2011-08-01 15:17:17

iPhone開發(fā) 證書 簽名

2011-08-19 10:05:30

iPhone開發(fā)

2011-07-18 09:35:29

iPhone 框架

2010-01-14 13:08:37

VB.NET運算符

2010-03-05 15:01:29

Python解析XML

2011-08-11 11:37:34

iPhone內(nèi)存

2011-06-02 17:27:49

iphone 多線程

2011-07-18 14:39:53

iPhone SDK UIKit

2025-04-16 10:03:40

開發(fā)Spring應(yīng)用程序

2011-07-22 18:25:20

XCode iPhone SDK

2009-12-02 10:49:59

PHP解析XML元素結(jié)
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 欧美一区二 | 亚洲国产精品一区 | 91久久精 | 亚洲免费毛片 | 久精品久久| 午夜免费视频观看 | 范冰冰一级做a爰片久久毛片 | 男人视频网站 | 51ⅴ精品国产91久久久久久 | 国产精品久久久久久久久久久久 | 国产一区二区三区免费 | 精品一区久久 | 国产成人午夜电影网 | 免费久久99精品国产婷婷六月 | 亚洲国产成人精品女人久久久 | 秋霞精品 | 中文字幕国产一区 | 四虎成人免费电影 | 国产精品一区二区三区免费观看 | 精品国产一区二区三区久久 | 中文字幕一区在线观看视频 | 精品视频在线观看 | 久在线| 在线三级网址 | 精品久久久久国产免费第一页 | 国产二区在线播放 | 精品欧美激情精品一区 | 欧美v日韩 | 免费成年网站 | 日韩视频国产 | 日韩三级在线 | 亚洲bt 欧美bt 日本bt | 午夜电影网 | 日日摸日日添日日躁av | 日本特黄a级高清免费大片 国产精品久久性 | 日韩影院一区 | 日韩一区二区福利视频 | 色爱综合网 | 性色在线| av在线播放网站 | 精品中文在线 |