一、调整项目的结构,导入必要的素材
  调整后的项目结构如下:

20161491512776.png (259×444)

二、新建两个控制器
(1)新建一个控制器,用于展示音乐文件列表界面,其继承自UITableViewController

20161491546838.png (511×136)

(2)新建一个控制器,用于展示播放界面,其继承自UIViewController

20161491603783.png (507×135)

(3)在storyboard中,把之前的控制器删除,换上一个导航控制器,设置tableViewController与之前新建的控制器类进行关联

20161491621225.png (683×341)

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

20161491645285.png (654×211)
音乐模型的代码如下:
YYMusicModel.h文件

//

//  YYMusicModel.h

//  20-音频处理(音乐播放器1)

//

//  Created by apple on 14-8-13.

//  Copyright (c) 2014年 yangyong. All rights reserved.

//
#import 
@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)使用字典转模型的第三方框架

20161491705409.png (208×143)

部分相关代码如下:

20161491725512.png (567×355)

此时的界面显示效果为:

20161491742778.png (316×494)

(3)添加一个UIimageView的分类,调整歌手的头像(正方形——>圆形)
  分类的实现代码如下:
  UIImage YY.h文件

#import 

  

@interface UIImage (YY)

  (instancetype)circleImageWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor;

@end

  UIImage YY.m文件
#import "UIImage YY.h"

#import 
@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

分类的使用:

20161491759734.png (814×366)

实现的效果:

20161491818008.png (315×492)

(4)推荐使用一个第三方框架,用来处理颜色

20161491840633.png (193×146)

涉及的代码:

20161491858112.png (761×141)

四、实现代码
  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 

@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

实现效果:

20161491930658.png (318×500)

六、补充说明

需要注意的细节处理

(1)UIImageView的分类,方形图片剪为圆形

(2)颜色的处理,文章中推荐的颜色处理框架提供了大量的颜色。

(3)取消选中被点击的这行cell.

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

(4)tableViewCell的封装

七、跳转
1.跳转到音乐播放界面的方法选择
  (1)使用模态跳转(又分为手动的和自动的)
  (2)使用xib并设置跳转
2.两种方法的分析
  可以使用模态的方法,添加一个控制器,让这个控制器和音乐播放控制器类进行关联,脱线,设置标识符且在cell的点击事件中执行segue即可。
  步骤说明:
  (1)在storyboard中新拖入一个控制器,然后设置和playing控制器类相关联。

20161491951708.png (1089×158)

(2)设置手动跳转

20161492016251.png (505×407)

(3)设置segue的标识符

20161492039138.png (256×139)

(3)跳转代码处理

20161492055780.png (724×218)

不推荐使用模态的原因如下:
    当选中一首音乐跳转到播放界面进行播放后,如果要跳回到音乐列表界面,那么最常见的做法是在音乐播放控制器上添加一个按钮。
    当点击的时候,销毁这个控制器(dismissed)。但是,控制器销毁了那么正在播放的音乐也就随之不在了。
    且由于播放界面控制器的布局是固定的,因此这里选择的方法是使用xib进行创建。
3.选择的方法
  新建一个xib,对应于音乐播放控制器。
  xib的结构如下图所示:

20161492112778.png (734×609)

细节:控制器只需要创建一次,因此建议使用懒加载,当然也可是把播放器设置为单例

//

//  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设置为半透明的,设置方法如下:

20161492131283.png (249×140)

设置为30%

20161492146732.png (213×361)

注意:不要再storyboard中控件的属性面板上设置透明度(这样的话,这个控件中的子控件也是同样的透明度)。
    不推荐的做法:

20161492200046.png (251×148)

(3)按钮点击发光

20161492222793.png (241×103)

(4)设置view隐藏能够节省一些性能。(参考代码)
(5)在切换控制器的过程中,设置窗口不能点击(这样做是为了防止用户多次连续的点击歌曲名会出现的问题)。
 
5.补充:
  项目代码中拖入了UIView的分类,以方便计算frame
 
6.涉及到的代码
在播放控制器的.h文件中提供一个公共对象方法接口
YYPlayingViewController.h文件

//  YYPlayingViewController.h
#import 
@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];

}

实例解析iOS中音乐播放器应用开发的基本要点的更多相关文章

  1. HTML5在微信内置浏览器下右上角菜单的调整字体导致页面显示错乱的问题

    HTML5在微信内置浏览器下,在右上角菜单的调整字体导致页面显示错乱的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

  2. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. ios – containerURLForSecurityApplicationGroupIdentifier:在iPhone和Watch模拟器上给出不同的结果

    我使用默认的XCode模板创建了一个WatchKit应用程序.我向iOSTarget,WatchkitAppTarget和WatchkitAppExtensionTarget添加了应用程序组权利.(这是应用程序组名称:group.com.lombax.fiveminutes)然后,我尝试使用iOSApp和WatchKitExtension访问共享文件夹URL:延期:iOS应用:但是,测试NSURL

  4. ios – Testflight无法安装应用程序

    我有几个测试人员注册了testflight并连接了他们的设备……他们有不同的ios型号……但是所有这些都有同样的问题.当他们从“safari”或“testflight”应用程序本身单击应用程序的安装按钮时……达到约90%并出现错误消息…

  5. ibm-mobilefirst – 在iOS 7.1上获取“无法安装应用程序,因为证书无效”错误

    当我的客户端将他们的设备更新到iOS7.1,然后尝试从AppCenter更新我们的应用程序时,我收到了上述错误.经过一番搜索,我找到了一个类似问题的帖子here.但是后来因为我在客户端使用AppCenter更新应用程序的环境中,我无法使用USB插件并为他们安装应用程序.在发布支持之前,是否有通过AppCenter进行下载的解决方法?

  6. ios – 视图的简单拖放?

    我正在学习iOS,但我找不到如何向UIView添加拖放行为.我试过了:它说“UIView没有可见的接口声明选择器addTarget”此外,我尝试添加平移手势识别器,但不确定这是否是我需要的它被称为,但不知道如何获得事件的坐标.在iOS中注册移动事件回调/拖放操作的标准简单方法是什么?

  7. ios – 什么控制iTunes中iPhone应用程序支持的语言列表?

    什么控制iPhone应用程序的iTunes页面中支持的语言?

  8. ios – 获得APNs响应BadDeviceToken或Unregistered的可能原因是什么?

    我知道设备令牌在某些时候是有效的.用户如何使其设备令牌变坏?从关于“未注册”的文档:Thedevicetokenisinactiveforthespecifiedtopic.这是否意味着应用程序已被删除?.您应该看到四种分发方法:如果您选择AppStore或Enterprise,您将在后面的对话框中看到Xcode将APNS权利更改为生产:如果选择AdHoc或Development,则aps-environment下的文本将是开发,然后应与后端的配置匹配.

  9. ios – 当我关闭应用程序时,我从调试器获得消息:由于信号15而终止

    我怎么能解决这个问题,我不知道这个链接MypreviousproblemaboutCoredata对我的问题有影响吗?当我cmd应用程序的Q时,将出现此消息.Messagefromdebugger:Terminatedduetosignal15如果谁知道我以前的问题的解决方案,请告诉我.解决方法>来自调试器的消息:每当用户通过CMD-Q(退出)或STOP手动终止应用程序(无论是在iOS模拟器中还是

  10. ios – NSUbiquityIdentityDidChangeNotification和SIGKILL

    当应用程序被发送到后台时,我们会删除观察者吗?我遇到的问题是,当UbiquityToken发生变化时,应用程序终止,因为用户已经更改了iCloud设置.你们如何设法订阅这个通知,如果你不这样做,你会做什么来跟踪当前登录的iCloud用户?

随机推荐

  1. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. iOS – genstrings:无法连接到输出目录en.lproj

    使用我桌面上的项目文件夹,我启动终端输入:cd然后将我的项目文件夹拖到终端,它给了我路径.然后我将这行代码粘贴到终端中找.-name*.m|xargsgenstrings-oen.lproj我在终端中收到此错误消息:genstrings:无法连接到输出目录en.lproj它多次打印这行,然后说我的项目是一个目录的路径?没有.strings文件.对我做错了什么的想法?

  3. iOS 7 UIButtonBarItem图像没有色调

    如何确保按钮图标采用全局色调?解决方法只是想将其转换为根注释,以便为“回答”复选标记提供更好的上下文,并提供更好的格式.我能想出这个!

  4. ios – 在自定义相机层的AVFoundation中自动对焦和自动曝光

    为AVFoundation定制图层相机创建精确的自动对焦和曝光的最佳方法是什么?

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 在没有iPhone6s或更新的情况下测试ARKit

    我在决定下载Xcode9之前.我想玩新的框架–ARKit.我知道要用ARKit运行app我需要一个带有A9芯片或更新版本的设备.不幸的是我有一个较旧的.我的问题是已经下载了新Xcode的人.在我的情况下有可能运行ARKit应用程序吗?那个或其他任何模拟器?任何想法或我将不得不购买新设备?解决方法任何iOS11设备都可以使用ARKit,但是具有高质量AR体验的全球跟踪功能需要使用A9或更高版本处理器的设备.使用iOS11测试版更新您的设备是必要的.

  7. 将iOS应用移植到Android

    我们制作了一个具有2000个目标c类的退出大型iOS应用程序.我想知道有一个最佳实践指南将其移植到Android?此外,由于我们的应用程序大量使用UINavigation和UIView控制器,我想知道在Android上有类似的模型和实现.谢谢到目前为止,guenter解决方法老实说,我认为你正在计划的只是制作难以维护的糟糕代码.我意识到这听起来像很多工作,但从长远来看它会更容易,我只是将应用程序的概念“移植”到android并从头开始编写.

  8. ios – 在Swift中覆盖Objective C类方法

    我是Swift的初学者,我正在尝试在Swift项目中使用JSONModel.我想从JSONModel覆盖方法keyMapper,但我没有找到如何覆盖模型类中的Objective-C类方法.该方法的签名是:我怎样才能做到这一点?解决方法您可以像覆盖实例方法一样执行此操作,但使用class关键字除外:

  9. ios – 在WKWebView中获取链接URL

    我想在WKWebView中获取tapped链接的url.链接采用自定义格式,可触发应用中的某些操作.例如HTTP://我的网站/帮助#深层链接对讲.我这样使用KVO:这在第一次点击链接时效果很好.但是,如果我连续两次点击相同的链接,它将不报告链接点击.是否有解决方法来解决这个问题,以便我可以检测每个点击并获取链接?任何关于这个的指针都会很棒!解决方法像这样更改addobserver在observeValue函数中,您可以获得两个值

  10. ios – 在Swift的UIView中找到UILabel

    我正在尝试在我的UIViewControllers的超级视图中找到我的UILabels.这是我的代码:这是在Objective-C中推荐的方式,但是在Swift中我只得到UIViews和CALayer.我肯定在提供给这个方法的视图中有UILabel.我错过了什么?我的UIViewController中的调用:解决方法使用函数式编程概念可以更轻松地实现这一目标.

返回
顶部