標籤:
iOS開發拓展篇—音頻處理(音樂播放器2)
說明:該文主要介紹音樂播放介面的搭建。
一、跳轉
1.跳轉到音樂播放介面的方法選擇 (1)使用模態跳轉(又分為手動的和自動的) (2)使用xib並設定跳轉
2.兩種方法的分析 可以使用模態的方法,添加一個控制器,讓這個控制器和音樂播放控制器類進行關聯,脫線,設定標識符且在cell的點擊事件中執行segue即可。 步驟說明: (1)在storyboard中新拖入一個控制器,然後設定和playing控制器類相關聯。 (2)設定手動跳轉 (3)設定segue的標識符 (3)跳轉代碼處理 不推薦使用模態的原因如下: 當選中一首音樂跳轉到播放介面進行播放後,如果要跳回到音樂列表介面,那麼最常見的做法是在音樂播放控制器上添加一個按鈕。 當點擊的時候,銷毀這個控制器(dismissed)。但是,控制器銷毀了那麼現正播放的音樂也就隨之不在了。 且由於播放介面控制器的布局是固定的,因此這裡選擇的方法是使用xib進行建立。
3.選擇的方法 建立一個xib,對應於音樂播放控制器。 xib的結構如所示: 細節:控制器只需要建立一次,因此建議使用懶載入,當然也可是把播放器設定為單例
1 // 2 // YYMusicsViewController.m 3 // 4 5 #import "YYMusicsViewController.h" 6 #import "YYMusicModel.h" 7 #import "MJExtension.h" 8 #import "YYMusicCell.h" 9 #import "YYPlayingViewController.h"10 11 @interface YYMusicsViewController ()12 @property(nonatomic,strong)NSArray *musics;13 @property(nonatomic,strong)YYPlayingViewController *playingViewController;14 @end15 16 @implementation YYMusicsViewController17 #pragma mark-懶載入18 -(NSArray *)musics19 {20 if (_musics==nil) {21 _musics=[YYMusicModel objectArrayWithFilename:@"Musics.plist"];22 }23 return _musics;24 }25 -(YYPlayingViewController *)playingViewController26 {27 if (_playingViewController==nil) {28 _playingViewController=[[YYPlayingViewController alloc]init];29 }30 return _playingViewController;31 }
4.xib的內部細節:(1)已經實現了約束,用於適配ios6和ios7。(2)設定音樂名稱和歌手的View設定為半透明的,設定方法如下: 設定為30% 注意:不要再storyboard中控制項的屬性面板上設定透明度(這樣的話,這個控制項中的子控制項也是同樣的透明度)。 不推薦的做法: (3)按鈕點擊發光 (4)設定view隱藏能夠節省一些效能。(參考代碼)(5)在切換控制器的過程中,設定視窗不能點擊(這樣做是為了防止使用者多次連續的點擊歌曲名會出現的問題)。 5.補充: 項目代碼中拖入了UIView的分類,以方便計算frame
二、涉及到的代碼在播放控制器的.h檔案中提供一個公用對象方法介面YYPlayingViewController.h檔案
1 // YYPlayingViewController.h2 3 #import <UIKit/UIKit.h>4 5 @interface YYPlayingViewController : UIViewController6 //顯示控制器7 -(void)show;8 @end
YYPlayingViewController.m檔案
1 // 2 // YYPlayingViewController.m 3 // 4 5 #import "YYPlayingViewController.h" 6 7 @interface YYPlayingViewController () 8 - (IBAction)exit; 9 10 @end11 12 @implementation YYPlayingViewController13 #pragma mark-公用方法14 -(void)show15 {16 //1.禁用整個app的點擊事件17 UIWindow *window=[UIApplication sharedApplication].keyWindow;18 window.userInteractionEnabled=NO;19 20 //2.添加播放介面21 //設定View的大小為覆蓋整個視窗22 self.view.frame=window.bounds;23 //設定view顯示24 self.view.hidden=NO;25 //把View添加到視窗上26 [window addSubview:self.view];27 28 //3.使用動畫讓View顯示29 self.view.y=self.view.height;30 [UIView animateWithDuration:0.25 animations:^{31 self.view.y=0;32 } completion:^(BOOL finished) {33 window.userInteractionEnabled=YES;34 }];35 }36 #pragma mark-內部的按鈕監聽方法37 //返回按鈕38 - (IBAction)exit {39 //1.禁用整個app的點擊事件40 UIWindow *window=[UIApplication sharedApplication].keyWindow;41 window.userInteractionEnabled=NO;42 43 //2.動畫隱藏View44 [UIView animateWithDuration:0.25 animations:^{45 self.view.y=window.height;46 } completion:^(BOOL finished) {47 window.userInteractionEnabled=YES;48 //設定view隱藏能夠節省一些效能49 self.view.hidden=YES;50 }];51 }52 @end
cell的點擊事件中的處理代碼:
1 /** 2 * cell的點擊事件 3 */ 4 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 5 { 6 //取消選中被點擊的這行 7 [tableView deselectRowAtIndexPath:indexPath animated:YES]; 8 9 //調用公用方法10 [self.playingViewController show];11 12 // //執行segue跳轉13 // [self performSegueWithIdentifier:@"music2playing" sender:nil];14 }
iOS開發拓展篇—音頻處理(音樂播放器2)