您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

iOS 音樂播放器開發實操:從列表頁到播放頁的完整流程

編輯:iOS 相關教學

一、先整理專案結構,把素材放進來

動手寫程式碼之前,先把圖片、歌曲、歌詞等資源歸位,後續引用才不會手忙腳亂。

二、建立兩個控制器

一個負責歌曲列表,一個負責播放畫面:

(1)列表控制器,繼承自 UITableViewController。

(2)播放控制器,繼承自 UIViewController。

(3)回到 storyboard,刪掉專案自帶的預設控制器,換成一個導航控制器,並把 tableViewController 與剛剛建立的列表控制器類關聯起來。

三、列表頁的基礎界面

(1)先有資料模型。依照 plist 的欄位設計 YYMusicModel:

YYMusicModel.h:


//
//  YYMusicModel.h
//  20-音頻處理(音樂播放器1)
//
//  Created by apple on 14-8-13.
//  Copyright (c) 2014年 yangyong. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface YYMusicModel : NSObject
/**
 *  歌曲名字
 */
@property (copy, nonatomic) NSString *name;
/**
 *  歌曲大圖
 */
@property (copy, nonatomic) NSString *icon;
/**
 *  歌曲的文件名
 */
@property (copy, nonatomic) NSString *filename;
/**
 *  歌詞的文件名
 */
@property (copy, nonatomic) NSString *lrcname;
/**
 *  歌手
 */
@property (copy, nonatomic) NSString *singer;
/**
 *  歌手圖標
 */
@property (copy, nonatomic) NSString *singerIcon;
@end

(2)字典轉模型這件事沒必要自己硬寫,直接引入成熟的第三方框架更省事。

相關程式碼:

此時列表已經能跑起來:

(3)歌手頭像是個正方形,視覺上有點生硬。與其到處寫裁剪邏輯,不如給 UIImage 加一個分類,把「方圖轉圓圖」這件事收攏成一個方法。

UIImage+YY.h:

#import <UIKit/UIKit.h>

@interface UIImage (YY)
+ (instancetype)circleImageWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor;
@end

UIImage+YY.m:

#import "UIImage+YY.h"
#import <objc/message.h>

@implementation UIImage (YY)
+ (instancetype)circleImageWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor
{
    // 1.加載原圖
    UIImage *oldImage = [UIImage imageNamed:name];
   
    // 2.開啟上下文
    CGFloat imageW = oldImage.size.width + 2 * borderWidth;
    CGFloat imageH = oldImage.size.height + 2 * borderWidth;
    CGSize imageSize = CGSizeMake(imageW, imageH);
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
   
    // 3.取得當前的上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
   
    // 4.畫邊框(大圓)
    [borderColor set];
    CGFloat bigRadius = imageW * 0.5;
    CGFloat centerX = bigRadius;
    CGFloat centerY = bigRadius;
    CGContextAddArc(ctx, centerX, centerY, bigRadius, 0, M_PI * 2, 0);
    CGContextFillPath(ctx);
   
    // 5.小圓
    CGFloat smallRadius = bigRadius - borderWidth;
    CGContextAddArc(ctx, centerX, centerY, smallRadius, 0, M_PI * 2, 0);
    CGContextClip(ctx);
   
    // 6.畫圖
    [oldImage drawInRect:CGRectMake(borderWidth, borderWidth, oldImage.size.width, oldImage.size.height)];
   
    // 7.取圖
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
   
    // 8.結束上下文
    UIGraphicsEndImageContext();
   
    return newImage;
}
@end

分類的呼叫方式:

效果對比很明顯:

(4)顏色也別再一個個手調 RGB,用現成的顏色框架能省下大量時間。

涉及的程式碼:

四、列表控制器的實作

YYMusicsViewController.m:


//
//  YYMusicsViewController.m
//  20-音頻處理(音樂播放器1)
//
//  Created by apple on 14-8-13.
//  Copyright (c) 2014年 yangyong. All rights reserved.
//

#import "YYMusicsViewController.h"
#import "YYMusicModel.h"
#import "MJExtension.h"
#import "UIImage+YY.h"
#import "Colours.h"

@interface YYMusicsViewController ()
@property(nonatomic,strong)NSArray *musics;
@end


@implementation YYMusicsViewController
#pragma mark-懶加載
-(NSArray *)musics
{
    if (_musics==nil) {
        _musics=[YYMusicModel objectArrayWithFilename:@"Musics.plist"];
    }
    return _musics;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

#pragma mark - Table view data source
/**
 *一共多少組
 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

/**
 *每組多少行
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.musics.count;
}

/**
 *每組每行的cell
 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID=@"ID";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    //取出數據模型
    YYMusicModel *model=self.musics[indexPath.row];
    cell.textLabel.text=model.name;
    cell.detailTextLabel.text=model.singer;
    cell.imageView.image=[UIImage circleImageWithName:model.singerIcon borderWidth:1 borderColor:[UIColor skyBlueColor]];
    return cell;
}

/**
 *  設置每個cell的高度
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

/**
 *  cell的點擊事件
 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

五、把 cell 的細節封裝起來

上面那個 cellForRow 裡混了取值、裁圓、上色三件事,控制器知道得太多。新建一個 YYMusicCell 繼承自 UITableViewCell,把這些細節關進去。

YYMusicCell.h:


#import <UIKit/UIKit.h>
@class YYMusicModel;
@interface YYMusicCell : UITableViewCell
+(instancetype)cellWithTableView:(UITableView *)tableView;
@property(nonatomic,strong)YYMusicModel *music;
@end

YYMusicCell.m:


#import "YYMusicCell.h"
#import "YYMusicModel.h"
#import "Colours.h"
#import "UIImage+YY.h"

@implementation YYMusicCell
//返回一個cell
+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID=@"ID";
    YYMusicCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell=[[YYMusicCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    return cell;
}

-(void)setMusic:(YYMusicModel *)music
{
    _music=music;
    self.textLabel.text=music.name;
    self.detailTextLabel.text=music.singer;
    self.imageView.image=[UIImage circleImageWithName:music.singerIcon borderWidth:1 borderColor:[UIColor skyBlueColor]];
}
@end

控制器隨之瘦身:


#import "YYMusicsViewController.h"
#import "YYMusicModel.h"
#import "MJExtension.h"
#import "YYMusicCell.h"

@interface YYMusicsViewController ()
@property(nonatomic,strong)NSArray *musics;
@end


@implementation YYMusicsViewController
#pragma mark-懶加載
-(NSArray *)musics
{
    if (_musics==nil) {
        _musics=[YYMusicModel objectArrayWithFilename:@"Musics.plist"];
    }
    return _musics;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.musics.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YYMusicCell *cell=[YYMusicCell cellWithTableView:tableView];
    cell.music=self.musics[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

視覺成果不變,可讀性大幅提升:

六、幾個容易被忽略的細節

(1)用 UIImageView 的分類把方圖裁圓。
(2)顏色統一走框架,避免同一個藍在不同頁面出現三種深淺。
(3)點擊後記得取消選中態:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
(4)cell 一定要封裝,這是所有列表頁的通用紀律。

七、跳轉到播放頁:兩種做法與取捨

常見的跳轉方式有兩類:模態跳轉(分手動與自動 segue),以及用 xib 加代碼顯示。

模態做法的步驟是:在 storyboard 拖入新控制器並關聯到播放控制器類,拉一條手動 segue,設定 identifier,然後在點擊事件裡執行跳轉。

(1)關聯控制器類:

(2)設為手動跳轉:

(3)設定 segue 的 identifier:

(4)在程式碼中觸發:

為什麼不推薦模態?想想使用情境:選一首歌進入播放頁,聽完想回列表,於是加個返回按鈕把播放控制器 dismiss 掉。問題就在這裡——控制器一銷毀,正在播放的音樂跟著消失,使用者體驗反倒變差。播放頁應該是常駐的、隨時可喚出的浮層,而不是一次性的頁面。

所以這裡改用 xib:控制器只建立一次,透過懶加載持有(也可以做成單例)。xib 的結構如下:

列表控制器加上懶加載:


#import "YYMusicsViewController.h"
#import "YYMusicModel.h"
#import "MJExtension.h"
#import "YYMusicCell.h"
#import "YYPlayingViewController.h"

@interface YYMusicsViewController ()
@property(nonatomic,strong)NSArray *musics;
@property(nonatomic,strong)YYPlayingViewController *playingViewController;
@end


@implementation YYMusicsViewController
#pragma mark-懶加載
-(NSArray *)musics
{
    if (_musics==nil) {
        _musics=[YYMusicModel objectArrayWithFilename:@"Musics.plist"];
    }
    return _musics;
}

-(YYPlayingViewController *)playingViewController
{
    if (_playingViewController==nil) {
        _playingViewController=[[YYPlayingViewController alloc]init];
    }
    return _playingViewController;
}

xib 內部還有幾個實用細節:

(1)已用 Auto Layout 做好約束,同時適配不同螢幕尺寸。
(2)歌曲名稱與歌手所在的容器設為半透明,透明度 30% 左右。

這裡有個容易踩的雷:不要在 storyboard 的屬性面板上直接調容器的透明度,因為子控制項會一起變淡。正確做法是只給容器本身設 alpha 或背景色透明度。

不推薦的做法:

(3)按鈕點擊時加一點高亮回饋。

(4)隱藏時把 view 設為 hidden,能省下不必要的效能開銷。
(5)切換過程中暫時禁用整個視窗的互動,防止使用者連點歌曲名導致視窗狀態錯亂。
(6)專案裡還引入了 UIView 的分類,方便直接讀寫 x、y、width、height。

播放控制器的頭文件對外只暴露一個方法:

YYPlayingViewController.h:

#import <UIKit/UIKit.h>

@interface YYPlayingViewController : UIViewController
//顯示控制器
-(void)show;
@end

YYPlayingViewController.m:


#import "YYPlayingViewController.h"

@interface YYPlayingViewController ()
- (IBAction)exit;

@end


@implementation YYPlayingViewController
#pragma mark-公共方法
-(void)show
{
    //1.禁用整個app的點擊事件
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    window.userInteractionEnabled=NO;
   
    //2.添加播放界面
    self.view.frame=window.bounds;
    self.view.hidden=NO;
    [window addSubview:self.view];
   
    //3.使用動畫讓View顯示
    self.view.y=self.view.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.view.y=0;
    } completion:^(BOOL finished) {
        window.userInteractionEnabled=YES;
    }];
}

#pragma mark-內部的按鈕監聽方法
//返回按鈕
- (IBAction)exit {
    //1.禁用整個app的點擊事件
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    window.userInteractionEnabled=NO;
   
    //2.動畫隱藏View
    [UIView animateWithDuration:0.25 animations:^{
        self.view.y=window.height;
    } completion:^(BOOL finished) {
        window.userInteractionEnabled=YES;
        //設置view隱藏能夠節省一些性能
        self.view.hidden=YES;
    }];
}

@end

最後在列表的點擊事件裡呼喚它:

/**
 *  cell的點擊事件
 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消選中被點擊的這行
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
    //調用公共方法
    [self.playingViewController show];
}

整條鏈路走通後可以看到:播放頁像一塊從底部滑出的浮層,關閉後音樂仍在背景繼續,這正是模態跳轉做不到的效果。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved