iOS筆記 (3)

十.忙碌指示器
    UIActivityIndicator。

1. UIActivityIndicatorView *activityIndicatior = UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge]; 

2.     activityIndicator.center = CGPointMake(512, 384); 

3.     [self.view addSubview: activityIndicator]; 

4.     [activityIndicator startAnimating];  

5.     [activityIndicator stopAnimating]; 

6.     [activityIndicator removeFromSuperView]; 

十一.媒體播放

1- 音頻
    [1] 音樂往往是存儲在iPod曲庫(注意位置)中的,可以通過媒體選擇器(media picker)或者媒體查詢(media query)讀取,然後用音樂播放器MPMusicPlayerController播放。

1. MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer]; 

2. [musicPlayer setShufleMode: MPMusicShuffleModeSongs]; 

3. [musicPlayer setRepeatMode: MPMusicRepeatModeAll]; 

4. [musicPlayer setQueueWithQuery: [MPMediaQuery songsQuery

5. [musicPlayer play]; 

   applicationMusicPlayer返回的播放器,在你的應用中播放音樂。它不會影響到iPod播放器,也不能從iPod播放器重獲取信息。
    iPodMusicPlayer返回的是iPod播放器,在你推出應用後,所有的設置都會影響到之後設備上的iPod播放器。
    獲得音樂播放器後,需要爲它設置一個播放隊列。可以用setQueueWithQuery:放方法,通過媒體查詢MPMediaQuery來設置 播放隊列,也可以用setQueueWithItemCollection:方法,通過MPMdiaItemCollection來設置播放隊列。
    重複模式repeatMode可以設置爲不重複、重複當前曲目、或整個播放列表;亂序播放shuffleMode可以設置爲不亂序、亂序播放曲目或亂序播放專輯;音量volume的設置與音頻播放器一樣。
    skipToNextItem跳到下一首,skipToPreviousItem跳到上一首,skipToBegin跳到第一首。
    對應的宏都是以MPMusic開頭。
    [2] 利用系統聲音服務來播放短暫音效(時長30秒以內),並震動:   

1. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);  

    播放指定音效:

1. NSURL *fileURL = [NSURL fileURLWithPath: path isDirectory: NO];  

2. // 創建音效ID 

3. SystemSoundID soundID; 

4. AudioServiceCreateSystemSoundID((CFURLRef) fileURL, &soundID); 

5. // 播放聲音 

6. AudioServicesPlaySystemSound(soundID); 

   [3] 音頻播放器
    沒有時長限制

1. NSURL *fileURL = [NSURL fileURLWithPath: path isDirectory: NO]; 

2. // URL來初始化音頻播放器-播放的不是iPod曲庫中的音樂 

3. AVAudioPlayer* player = AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: NO]; 

4. // 準備播放 

5. [player prepareToPlay]; 

6. // 設置代理 

7. [player setDelegate: self]; 

   方法:play、pause、stop。可以通過playing屬性查詢播放器是否正在播放當中,可以通過volume屬性來修改和查詢播放器的播放增益(從0.0到1.0),可通過setting屬性查詢播放器其他設置。

    duration表示音頻的時間長度, currentTime表示當前播放到的時間。播放結束後可以通過代理方法audioPlayerDidFinishPlaying:來處理播放後設置。
2- 視頻
    視頻播放可以採用網頁視圖(UIWebView)進行嵌入式播放(能播放YouTube視頻),或者採用電影播放器(MPMoviePlayerController)進行播放。
    [1] 電影播放器

1. MPMoviePlayerController *player = MPMoviePlayerController alloc]initWithContentURL: url]; 

2.     // 設置播放器的大小,並將其加入視圖中 

3.     [player.view setFrame: rectFrame]; 

4.     [self.view addSubView: player.view]; 

5.     播放器的背景視圖backgroundView 

6.     全屏[player setFullscreen: YES animated: YES]; 

7.     播放另一個影片[player setContentURL: newURL]; 

8.     [player requestThumbnailImagesAtTimes:arrayTimes timeOption:MPMovieTimeOptionNearestKeyFrame]; // 表示播放器不會在你所指定的時間去截取預覽,而是在絕對時間的附近幾幀中尋找效果最好的幀做爲預覽。 

scalingMode規定了影片的縮放模式。   

initialPlaybackTime用來控制視頻開始播放的時間,單位是秒。

如果視頻源在網絡上,那麼需要正確設置服務器端的mimeType。
十二.運行環-runloop
    運行環作用於一個iOS應用的整個生命週期。它負責監視各種輸入事件,並且在合適的時候對這些輸入進行分配。應用的每一個線程都有且僅有一個運行環。你自己不需要創建也不需要銷燬運行環,但是可以通過currentRunLoop方法來獲取當前的運行環。

十三.定時器
    由於運行環機制,定時器的精度不高,只能用於一般性延時。
    例子:拼圖遊戲,DeskViewController.m。
    NSObject類的定時方法。
    performSelector: withObject: afterDelay: 運行方法,參數,時間(秒)。
    performSelectorOnMainThread: withObject: waitUntilDone:在主線程中,運行參數selector所指定的方法,如果waitUntilDone參數爲YES,那麼當前線程會被阻攔,直到selector運行完。
    performSelector: onThread: withObject: waitUntilDone:同上,但不一定在主線程中運行。
    performSelectorInBackground: withObject: 開啓一個新線程,用於運行selector方法,selector方法應負責線程的初始化。
    cancelPreviousPerformRequestsWithTarget:取消與一個目標相關的所有計劃好的動作。
    cancelPreviousPerformRequestsWithTraget: selector:object只取消特定的計劃動作。
十四.隨機數
0~1之間隨機數

1. CG_INLINE float genRandomNum() 

2. 

3.     return (float)arc4random/ARCRANDOM_MAX; 

4. 

十五.加速度傳感器
    獲得加速度傳感器讀數:

1. UIAccelerometer *accelerometer = [UIAccelerometer shareAccelerometer]; // 單例 

2. accelerometer.delegate = self; 

3. accelerometer.updateInterval = 1.0f / 10.0f; // 傳感器更新頻率,普通遊戲10~30HZ,虛擬現實類遊戲,30~60HZ 

4. accelerometer.x, accelerometer.y, accelerometer.z // 傳感器讀數 

   若不想再接受傳感器讀數,可將代理設置爲nil

1. [UIAccelerometer shareAccelerometer].delegate = nil;  

2.  

3. #define alpha 0.05 

4. // 獲得重力引起的加速度讀數: 

5. - (void)accelerometer: (UIAccelerometer *)accelerometer didAccelerate: (UIAcceleration *)acceleration 

6. 

7.     // 簡單低通濾波器 

8.     accelX = (acceleration.x * alpha) + (accelX) * (1.0 - alpha); 

9.     accelY = (acceleration.y * alpha) + (accelY) * (1.0 - alpha); 

10.    accelZ = (acceleration.z * alpha) + (accelZ) * (1.0 - alpha); 

11.

12.     

13.// 獲得動作引起的加速度讀數: 

14.- (void)accelerometer: (UIAccelerometer *)accelerometer didAccelerate: (UIAcceleration *)acceleration 

15.

16.    // 減去低通濾波輸出,得到高通濾波輸出 

17.    accelX = acceleration.x - ((acceleration.x * alpha) + (accelX) * (1.0 - alpha)); 

18.    accelY = acceleration.y - ((acceleration.y * alpha) + (accelY) * (1.0 - alpha)); 

19.    accelZ = acceleration.z - ((acceleration.z * alpha) + (accelZ) * (1.0 - alpha)); 

20.

十六.地圖與連接服務器
[1] 添加框架MapKit.framework。使用MKMapView來呈現地圖。注意應當直接使用此類,而不是繼承之。如果希望在MKMapView類之上添加功能,可以使用MKMapViewDelegate協議。
    初始化:

1. MKMapView *mapView = MKMapView alloc] initWithFrame: rect]; 

   初始化之後並不直接顯示,還需要指定顯示的地圖區域:

1. CLLocationCoordinate2D coordinate; 

2. coordinate.latitude = latitudeValue; // 緯度 

3. coordinate.longtitude = longtitudeValue; // 精度 

4. mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, width, height); // 指定顯示區域,widthheight單位都是米 

   之後可以通過addSubview添加地圖。
    屬性:showsUserLocation-爲YES,系統會持續跟蹤用戶的位置
          userLocationVisible-爲YES,將顯示用戶所在位置
    顯示地圖之後,常常希望在地圖上添加標註,這需要創建一個類,並實現MKAnnotation協議,這個類叫做標註對象。標註對象往往實現setCoordinate:方法來設置其座標。在地圖視圖上,可以設置標註對象的座標,然後添加進去,這樣地圖上就會出現一個標註。代理方法title 和subtitle能夠在標註上顯示標題和副標題。  

1. // 初始化 

2.    mapView = MKMapView alloc] initWithFrame: CGRectMake(100, 100, 550, 700)]; 

3.    mapView.showsUserLocation = TRUE; 

4.    mapView.mapType = MKMapTypeStandard; 

5.    mapView.delegate = self; 

6.    // 設置座標 

7.    CLLocationCoordinate2D coordinate; 

8.    coordinate.latitude = 37.31; 

9.    coordinate.longtitude = -122.03; 

10.   mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 4000, 6000); // 4000米寬,6000米高的區域 

11.   [self.view insertSubview: mapView atIndex: 0]; 

十七.通過storyboard來初始化一個controller

1. CBigDesignImageViewController *imageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"BigImageController"];  

  BigImageController是要在storyboard中設置的Identifier屬性。

十八.給UIView子類設置陰影,包括各種控件
    1 導入QuartzCore framework
    2 #import <QuartzCore/QuartzCore.h>
    3 編碼:

1. imgView layer] setShadowOffset:CGSizeMake(5, 5)]; // 陰影的範圍  

2. imgView layer] setShadowRadius:2]; // 陰影擴散的範圍控制  

3. imgView layer] setShadowOpacity:1]; // 陰影透明度  

4. imgView layer] setShadowColor:[UIColor brownColor].CGColor]; // 陰影的顏色 

十九.設置UIScrollView滾動速度      

1. // 自動滾動太快,效果不好,這裏把動畫設置慢點,注意下面要直接賦值contentOffset,不要用帶animated參數的函數,否則動畫會出問題,因爲兩處都是動畫效果。 

2.         [UIScrollView animateWithDuration:1.0f  

3.                                delay:0  

4.                                options:UIViewAnimationCurveLinear 

5.                                animations:^{ 

6.                                    scrollView.contentOffset = CGPointMake(0, 0);  

7.                                } 

8.                                completion:^(BOOL finished){} 

9.                                ];  

10.        // 如果在減速滾動過程中,按了刷新按鈕,執行上面的動畫,會出現重置的位置,y不是0的情況,這裏再調用一次,滾動到0 

11.        [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];  

二十.EXC_BAD_ACCESS
Here’s the advice I generally give todevelopers when you hit an EXC_BAD_ACCESS error:
-Set the NSZombieEnabled argument in your executable options, which sometimeshelps narrow down the cause
-Run with Apple Instruments such as Leaks to look for memory issues
-Set a breakpoint in your code and step through until you narrow down where it’s crashing
Tried and true “comment out code till it works” then backtrack from there :]

Xcode4 下設置 NSZombieEnabled 的方法: 
你可以點擊 Xcode4 菜單 Product ->Edit Scheme -> Arguments, 然後將點擊”加號”, 將 NSZombieEnabled 參數加到 Environment Variables 窗口中, 後面的數值寫上 ”YES”.
或者在 Xcode4 菜單 Product ->Edit Scheme -> Diagnostics 設置窗口中直接勾上 Enable ZombieObjects 即可,Xcode 可用 cmd+shift+< 進到這個窗口。 
Xcode4 已經考慮到了現在的要求,所以提供了更便捷的設置的方式,你也可以在這個窗口中設置其他一些參數,你肯定能由此獲得更多的幫助信息。

 

好了,看完這篇iOS學習筆記整理,不知道對你是不是有所啓發

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章