激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - IOS - 實例解析iOS中音樂播放器應用開發的基本要點

實例解析iOS中音樂播放器應用開發的基本要點

2021-01-03 16:33文頂頂 IOS

這篇文章主要介紹了iOS開發中制作一個簡單的音樂播放器的基本要點解析,代碼基于傳統的Objective-C,需要的朋友可以參考下

一、調整項目的結構,導入必要的素材
  調整后的項目結構如下:

實例解析iOS中音樂播放器應用開發的基本要點

二、新建兩個控制器
(1)新建一個控制器,用于展示音樂文件列表界面,其繼承自uitableviewcontroller

實例解析iOS中音樂播放器應用開發的基本要點

(2)新建一個控制器,用于展示播放界面,其繼承自uiviewcontroller

實例解析iOS中音樂播放器應用開發的基本要點

(3)在storyboard中,把之前的控制器刪除,換上一個導航控制器,設置tableviewcontroller與之前新建的控制器類進行關聯

實例解析iOS中音樂播放器應用開發的基本要點

三、音樂文件列表控制器中基本界面的搭建
(1)新建一個音樂文件的模型
根據plist文件建立模型:

實例解析iOS中音樂播放器應用開發的基本要點
音樂模型的代碼如下:
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)使用字典轉模型的第三方框架

 

實例解析iOS中音樂播放器應用開發的基本要點

部分相關代碼如下:

實例解析iOS中音樂播放器應用開發的基本要點

此時的界面顯示效果為:

實例解析iOS中音樂播放器應用開發的基本要點

(3)添加一個uiimageview的分類,調整歌手的頭像(正方形——>圓形)
  分類的實現代碼如下:
  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


分類的使用:

 

實例解析iOS中音樂播放器應用開發的基本要點

實現的效果:

實例解析iOS中音樂播放器應用開發的基本要點

(4)推薦使用一個第三方框架,用來處理顏色

實例解析iOS中音樂播放器應用開發的基本要點

涉及的代碼:

實例解析iOS中音樂播放器應用開發的基本要點

四、實現代碼
  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


五、改進
  對tableviewcell的代碼進行封裝:
實現:新建一個yymusiccell類,繼承自uitableviewcell。
封裝代碼如下:
yymusiccell.h文件

復制代碼 代碼如下:


//
//  yymusiccell.h
//  20-音頻處理(音樂播放器1)
//
//  created by apple on 14-8-13.
//  copyright (c) 2014年 yangyong. all rights reserved.
//

 

#import <uikit/uikit.h>
@class yymusicmodel;
@interface yymusiccell : uitableviewcell
+(instancetype)cellwithtableview:(uitableview *)tableview;
@property(nonatomic,strong)yymusicmodel *music;
@end


yymusiccell.m文件

復制代碼 代碼如下:


//
//  yymusiccell.m
//  20-音頻處理(音樂播放器1)
//
//  created by apple on 14-8-13.
//  copyright (c) 2014年 yangyong. all rights reserved.
//

 

#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


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 "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;
}
/**
 *每組每行的cell
 */
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
{
    yymusiccell *cell=[yymusiccell cellwithtableview:tableview];
    cell.music=self.musics[indexpath.row];
    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


實現效果:

 

實例解析iOS中音樂播放器應用開發的基本要點

六、補充說明

需要注意的細節處理

(1)uiimageview的分類,方形圖片剪為圓形

(2)顏色的處理,文章中推薦的顏色處理框架提供了大量的顏色。

(3)取消選中被點擊的這行cell.

復制代碼 代碼如下:

    [tableview deselectrowatindexpath:indexpath animated:yes];


(4)tableviewcell的封裝

 

七、跳轉
1.跳轉到音樂播放界面的方法選擇
  (1)使用模態跳轉(又分為手動的和自動的)
  (2)使用xib并設置跳轉
2.兩種方法的分析
  可以使用模態的方法,添加一個控制器,讓這個控制器和音樂播放控制器類進行關聯,脫線,設置標識符且在cell的點擊事件中執行segue即可。
  步驟說明:
  (1)在storyboard中新拖入一個控制器,然后設置和playing控制器類相關聯。

實例解析iOS中音樂播放器應用開發的基本要點

(2)設置手動跳轉

實例解析iOS中音樂播放器應用開發的基本要點

(3)設置segue的標識符

實例解析iOS中音樂播放器應用開發的基本要點

(3)跳轉代碼處理

實例解析iOS中音樂播放器應用開發的基本要點

不推薦使用模態的原因如下:
    當選中一首音樂跳轉到播放界面進行播放后,如果要跳回到音樂列表界面,那么最常見的做法是在音樂播放控制器上添加一個按鈕。
    當點擊的時候,銷毀這個控制器(dismissed)。但是,控制器銷毀了那么正在播放的音樂也就隨之不在了。
    且由于播放界面控制器的布局是固定的,因此這里選擇的方法是使用xib進行創建。
3.選擇的方法
  新建一個xib,對應于音樂播放控制器。
  xib的結構如下圖所示:

實例解析iOS中音樂播放器應用開發的基本要點

細節:控制器只需要創建一次,因此建議使用懶加載,當然也可是把播放器設置為單例

復制代碼 代碼如下:


//
//  yymusicsviewcontroller.m
//

 

#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;
}


4.xib的內部細節:
(1)已經實現了約束,用于適配ios6和ios7。
(2)設置音樂名稱和歌手的view設置為半透明的,設置方法如下:

 

實例解析iOS中音樂播放器應用開發的基本要點

設置為30%

實例解析iOS中音樂播放器應用開發的基本要點

注意:不要再storyboard中控件的屬性面板上設置透明度(這樣的話,這個控件中的子控件也是同樣的透明度)。
    不推薦的做法:

實例解析iOS中音樂播放器應用開發的基本要點

(3)按鈕點擊發光

實例解析iOS中音樂播放器應用開發的基本要點

(4)設置view隱藏能夠節省一些性能。(參考代碼)
(5)在切換控制器的過程中,設置窗口不能點擊(這樣做是為了防止用戶多次連續的點擊歌曲名會出現的問題)。
 
5.補充:
  項目代碼中拖入了uiview的分類,以方便計算frame
 
6.涉及到的代碼
在播放控制器的.h文件中提供一個公共對象方法接口
yyplayingviewcontroller.h文件

復制代碼 代碼如下:


//  yyplayingviewcontroller.h

 

#import <uikit/uikit.h>

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


yyplayingviewcontroller.m文件

復制代碼 代碼如下:


//
//  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.添加播放界面
    //設置view的大小為覆蓋整個窗口
    self.view.frame=window.bounds;
    //設置view顯示
    self.view.hidden=no;
    //把view添加到窗口上
    [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的點擊事件中的處理代碼:

復制代碼 代碼如下:

/**
 *  cell的點擊事件
 */
-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath
{
    //取消選中被點擊的這行
    [tableview deselectrowatindexpath:indexpath animated:yes];
   
    //調用公共方法
    [self.playingviewcontroller show];
   
//    //執行segue跳轉
//    [self performseguewithidentifier:@"music2playing" sender:nil];
}

延伸 · 閱讀

精彩推薦
  • IOSiOS中UILabel實現長按復制功能實例代碼

    iOS中UILabel實現長按復制功能實例代碼

    在iOS開發過程中,有時候會用到UILabel展示的內容,那么就設計到點擊UILabel復制它上面展示的內容的功能,也就是Label長按復制功能,下面這篇文章主要給大...

    devilx12792021-04-02
  • IOSiOS中MD5加密算法的介紹和使用

    iOS中MD5加密算法的介紹和使用

    MD5加密是最常用的加密方法之一,是從一段字符串中通過相應特征生成一段32位的數字字母混合碼。對輸入信息生成唯一的128位散列值(32個字符)。這篇文...

    LYSNote5432021-02-04
  • IOSiOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和Masonry簡單使用)

    iOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和

    這篇文章主要介紹了iOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和Masonry簡單使用)的相關資料,需要的朋友可以參考下...

    CodingFire13652021-02-26
  • IOSiOS實現控制屏幕常亮不變暗的方法示例

    iOS實現控制屏幕常亮不變暗的方法示例

    最近在工作中遇到了要將iOS屏幕保持常亮的需求,所以下面這篇文章主要給大家介紹了關于利用iOS如何實現控制屏幕常亮不變暗的方法,文中給出了詳細的...

    隨風13332021-04-02
  • IOSiOS開發之視圖切換

    iOS開發之視圖切換

    在iOS開發中視圖的切換是很頻繁的,獨立的視圖應用在實際開發過程中并不常見,除非你的應用足夠簡單。在iOS開發中常用的視圖切換有三種,今天我們將...

    執著丶執念5272021-01-16
  • IOS詳解iOS中多個網絡請求的同步問題總結

    詳解iOS中多個網絡請求的同步問題總結

    這篇文章主要介紹了詳解iOS中多個網絡請求的同步問題總結,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧...

    liang199111302021-03-15
  • IOSiOS開發技巧之狀態欄字體顏色的設置方法

    iOS開發技巧之狀態欄字體顏色的設置方法

    有時候我們需要根據不同的背景修改狀態欄字體的顏色,下面這篇文章主要給大家介紹了關于iOS開發技巧之狀態欄字體顏色的設置方法,文中通過示例代碼...

    夢想家-mxj8922021-05-10
  • IOSiOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果

    iOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果

    這篇文章主要介紹了iOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果的相關資料,需要的朋友可以參考下...

    jiangamh8882021-01-11
主站蜘蛛池模板: 国产精品久久久久久久午夜片 | 成人免费视频视频在线观看 免费 | 黑人日比 | 日韩毛片免费观看 | 91成人午夜性a一级毛片 | 国产一区免费观看 | gogo全球大胆高清人露出91 | 国产黄色录像片 | 2019亚洲日韩新视频 | 日本黄色不卡视频 | 欧美日韩精品一区二区三区不卡 | 黄色片网站免费观看 | 久久九九热re6这里有精品 | 国产18视频 | 久久久久国产精品久久久久 | 99一区二区 | fc2成人免费人成在线观看播放 | 99精品视频一区二区三区 | 草久网| 蜜桃日韩| 性欧美videos 另类喷潮 | 久久精品欧美电影 | 精品国产91久久久 | 91久久99热青草国产 | 失禁高潮抽搐喷水h | 视频一区二区视频 | 久久一级 | 国产流白浆高潮在线观看 | 久久久无码精品亚洲日韩按摩 | 男女生羞羞视频网站在线观看 | 久久亚洲春色中文字幕久久 | 亚洲最大av网站 | 欧美性受xxxx白人性爽 | 欧美一区二区黄色片 | 毛片在线视频观看 | 久久久久久麻豆 | 免费观看视频在线 | 香蕉视频破解 | 国产一区视频观看 | 一区二区久久精品66国产精品 | 爽爽淫人综合网网站 |