zoukankan      html  css  js  c++  java
  • iOS开发拓展篇—音频处理(音乐播放器2)

    iOS开发拓展篇—音频处理(音乐播放器2)

    说明:该文主要介绍音乐播放界面的搭建。

    一、跳转

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

     

      (3)设置segue的标识符

     

      (3)跳转代码处理

     

    不推荐使用模态的原因如下:
        当选中一首音乐跳转到播放界面进行播放后,如果要跳回到音乐列表界面,那么最常见的做法是在音乐播放控制器上添加一个按钮。
        当点击的时候,销毁这个控制器(dismissed)。但是,控制器销毁了那么正在播放的音乐也就随之不在了。
        且由于播放界面控制器的布局是固定的,因此这里选择的方法是使用xib进行创建。
    3.选择的方法
      新建一个xib,对应于音乐播放控制器。
      xib的结构如下图所示:
    细节:控制器只需要创建一次,因此建议使用懒加载,当然也可是把播放器设置为单例
     
    //
    //  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设置为半透明的,设置方法如下:
      设置为30%

     

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

     

    (4)设置view隐藏能够节省一些性能。(参考代码)
    (5)在切换控制器的过程中,设置窗口不能点击(这样做是为了防止用户多次连续的点击歌曲名会出现的问题)。
     
    5.补充:
      项目代码中拖入了UIView的分类,以方便计算frame
     
    二、涉及到的代码
    在播放控制器的.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];
    }
  • 相关阅读:
    codeforces#571Div2 D---Vus the Cossack and Numbers【贪心】
    洛谷P1050 循环【java大数】
    洛谷P1972 HH的项链【树状数组】
    uoj#67 新年的毒瘤【Tarjan】
    洛谷1265 公路修建【最小生成树】
    【超实用工具】三维场景绘制工具
    坐标地址批处理工具
    CAD转KML乱码处理
    地理编码逆编码教程
    最新!全球ALOS 12m地形数据介绍及下载
  • 原文地址:https://www.cnblogs.com/yipingios/p/5566714.html
Copyright © 2011-2022 走看看