美文网首页
Day 3 - 播放本地视频,闭包,代理

Day 3 - 播放本地视频,闭包,代理

作者: Codepgq | 来源:发表于2016-09-07 17:07 被阅读48次

    先看效果图

    最终效果

    Day 2用的是拖线,这里使用纯代码完成

    <br />

    1、创建工程、搭建UI

    创建工程不说了!
    直接搭建UI
    分析UI,一个tableView,自定的Cell

    1.1创建tableView

    var tableView = UITableView()
    或者
    var tableview :UITableView? = UITableView()
    

    1.2创建Cell

    选择Swift

    下一步,名字随便你

    观察可得知Cell至少包含

    UIImageView,UIbutton ,UIlabel(两个)

    所以在你的Swift中申明一下变量

        var icon: UIImageView?
        var titleLabel : UILabel?
        var deTitleLabel: UILabel?
        var playBtn : UIButton?
    

    1.3 创建icon、titleLabel,deTitleLabel、playBtn

    在init init(style: UITableViewCellStyle, reuseIdentifier: String?)方法中创建
    在layoutSubViews中布局

    override func layoutSubviews() {
            super.layoutSubviews()
            
            icon?.frame = self.contentView.bounds
            playBtn?.bounds = CGRectMake(0, 0, 50, 50)
            playBtn?.frame.origin = CGPointMake((self.contentView.bounds.size.width - 50) / 2, (self.contentView.bounds.size.height - 50) / 2)
            playBtn?.setImage(UIImage.init(named: "playBtn"), forState: UIControlState.Normal)
            
            titleLabel?.frame = CGRectMake(0, self.contentView.bounds.size.width * 0.5 - 10, self.contentView.bounds.size.width, 20);
            deTitleLabel?.frame = CGRectMake(0, self.contentView.bounds.size.width * 0.5 + 20, self.contentView.bounds.size.width, 20);
         
        }
        
        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            playBtn = UIButton.init(type: UIButtonType.Custom)
            playBtn?.addTarget(self, action: #selector(TableViewCell.click(_:)), forControlEvents: UIControlEvents.TouchUpInside);
            
            icon = UIImageView.init()
            titleLabel = UILabel.init();
            titleLabel?.textAlignment = NSTextAlignment.Center
            titleLabel?.textColor = UIColor.whiteColor()
            deTitleLabel = UILabel.init();
            deTitleLabel?.textAlignment = NSTextAlignment.Center
            deTitleLabel?.textColor = UIColor.whiteColor()
            self.contentView.addSubview(icon!)
            self.contentView.addSubview(playBtn!)
            self.contentView.addSubview(titleLabel!)
            self.contentView.addSubview(deTitleLabel!)
        }
    

    上面的代码基本上就是设置大小,位置

    textAlignment:文字对齐方式
    textColor:文字颜色
    addSubView()添加到View中
    addTarget:添加事件
    frame大小位置
    bounds:大小 x y 一般写0
    init()初始化
    

    实现按钮点击方法:不然会报错

    func click(button:UIButton){
            print("click me");
    }
    

    1.4、创建TableView,添加到View中,设置代理,实现代理,创建数据源,提前注册Cell,

    与下面代码一一对应

        tableView = UITableView.init(frame: self.view.bounds, style: UITableViewStyle.Plain)
            tableView.delegate = self;
            tableView.dataSource = self;
            self.view.addSubview(tableView);
            
            tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "Cell")
    
    

    导入代理:

    class ViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate 
    

    实现代理方法

     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 10
        }
        
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
            cell.delegate = self
            
            return cell;
        }
    

    2、设置数据源

    在1中完成了自定义Cell,使用代码创建TableView,但是运行并没有数据
    接下来就是为Cell设置数据源
    在Cell中添加一个方法

     func initData(imageNamed : NSString , title : NSString , deTitle : NSString){
            icon?.image = UIImage.init(named: imageNamed as String)
            titleLabel?.text = String(title)
            deTitleLabel?.text = deTitle as String
        }
    

    这里我们需要图片名称、标题、时间
    所以我们创建一个结构体在Swift中

    struct video {
        let imageNamed: String
        let title: String
        let time:String
        
    }
    

    在把数据源添加好:

    var data : Array = [video(imageNamed: "videoScreenshot01", title: "Introduce 3DS Mario", time: "Youtube - 06:32"),
                            video(imageNamed: "videoScreenshot02", title: "Emoji Among Us", time: "Vimeo - 3:34"),
                            video(imageNamed: "videoScreenshot03", title: "Seals Documentary", time: "Vine - 00:06"),
                            video(imageNamed: "videoScreenshot04", title: "Adventure Time", time: "Youtube - 02:39"),
                            video(imageNamed: "videoScreenshot05", title: "Facebook HQ", time: "Facebook - 10:20"),
                            video(imageNamed: "videoScreenshot06", title: "Lijiang Lugu Lake", time: "Allen - 20:30")]
    

    然后对tableVIew的方法进行修改

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return data.count
        }
        
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
            cell.delegate = self
            let model = self.data[indexPath.row];
            
            cell.initData(model.imageNamed, title: model.title, deTitle: model.time)
    

    到这里为止:我们完成了展示,可是我们点击并不能播放,所以实现最后一步:
    回调

    4、回调

    • 4.1 闭包:

    4.1.1 申明一个闭包:

    typealias buttonClickFunc = (button:UIButton)->Void
    

    4.1.2 申明一个变量

    var myBtnClickBlock = buttonClickFunc?()
    

    4.1.3 添加一个方法

    func myPlayBtnClickBlock(block:(button:UIButton)->Void){
            myBtnClickBlock = block
        }
    

    4.1.4 在按钮点击事件中调用

    func click(button:UIButton){
            print("click me");
            
            //如果block不存在就不调用
            if (myBtnClickBlock != nil) {
                myBtnClickBlock!(button: button)
            }
        }
    

    4.1.5 在ViewController中修改方法

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
            cell.delegate = self
            let model = self.data[indexPath.row];
            
            cell.initData(model.imageNamed, title: model.title, deTitle: model.time)
           
            cell.myPlayBtnClickBlock { (button) in
                print("我是VC")
            }
            return cell;
        }
    

    到这里你就会发现能打印“我是VC”,这行了,然后我们就可以开始播放视频了,

    • 播放视频之前要把视频导入到项目工程中,项目最后会把资源上传

    写一个方法专门用来播放视频
    先导入必要框架

    import AVKit
    import AVFoundation
    

    申明对象:

    var playViewController = AVPlayerViewController()
    var playerView = AVPlayer()
    

    播放视频的实现

    func playVideo(){
                    
            let path = NSBundle.mainBundle().pathForResource("emoji zone", ofType: "mp4")
            
            self.playerView = AVPlayer(URL: NSURL(fileURLWithPath: path!))
            
            self.playViewController.player = self.playerView
            
            self.presentViewController(self.playViewController, animated: true) {
                self.playViewController.player?.play()
            }
        }
    path :加载视频路径
    ..下面的就是创建播放对象,然后present到一个页面,进行播放...
    
    • 4.2使用代理

    申明代理:

    protocol myBtnClickCellDelegate : class {
        func cellPlayBtnClick(cell : TableViewCell, button : UIButton)
    }
    
    这里需要注意:创建一个weak的变量
    weak var delegate : myBtnClickCellDelegate?
    

    在Viewcontroller中导入代理

    class ViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate ,myBtnClickCellDelegate
    

    实现代理方法:你不实现会报错的,啊哈哈哈😝

    func cellPlayBtnClick(cell: TableViewCell, button: UIButton) {
            print("进入代理了!!!")
            playVideo()
        }
    

    在点击按钮的时候调用代理

    func click(button:UIButton){
            print("click me");
            delegate?.cellPlayBtnClick(self, button: button)
    
        }
    

    然后就可以实现了!!!
    最后把

    Demo奉上

    百度云

    相关文章

      网友评论

          本文标题:Day 3 - 播放本地视频,闭包,代理

          本文链接:https://www.haomeiwen.com/subject/hpijettx.html