iOS開發拓展篇—音頻處理(音樂播放器2)

iOS開發拓展篇—音頻處理(音樂播放器2)ios

說明:該文主要介紹音樂播放界面的搭建。app

1、跳轉佈局

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 @end
15 
16 @implementation YYMusicsViewController
17 #pragma mark-懶加載
18 -(NSArray *)musics
19 {
20     if (_musics==nil) {
21         _musics=[YYMusicModel objectArrayWithFilename:@"Musics.plist"];
22     }
23     return _musics;
24 }
25 -(YYPlayingViewController *)playingViewController 26 { 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
 
2、涉及到的代碼
在播放控制器的.h文件中提供一個公共對象方法接口
YYPlayingViewController.h文件
1 //  YYPlayingViewController.h
2 
3 #import <UIKit/UIKit.h>
4 
5 @interface YYPlayingViewController : UIViewController
6 //顯示控制器
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 @end
11 
12 @implementation YYPlayingViewController
13 #pragma mark-公共方法
14 -(void)show
15 {
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.動畫隱藏View
44     [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 }
相關文章
相關標籤/搜索