自學 iOS,自學ios

來源:互聯網
上載者:User

自學 iOS,自學ios

做了這個小demo 之後  感覺OC 和swift 還是有很大的差別的 自己還是要去多看些swift的文法 用的不是很熟練 

1.這個demo 的資源檔 我都是用原工程的 

2.同樣的自訂cell 的時候 用的是“SnapKit”這個庫

3.其實這一個demo的主要就是自訂cell,思想和OC 是一樣的 總感覺swift寫的是那麼的彆扭,可能還是不熟悉文法吧,還是要多看多練

效果

 

代碼 自訂cell 的代碼

import UIKitimport SnapKitstruct video {    let image: String    let title: String    let source: String    }class VideoViewCell: UITableViewCell {    public var titleLabel:UILabel?    public var picImageView:UIImageView?    public var startImageView:UIImageView?        override func awakeFromNib() {        super.awakeFromNib()            }        required init?(coder aDecoder: NSCoder) {        super.init(coder: aDecoder)    }        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {        super.init(style: style, reuseIdentifier: reuseIdentifier);        self.setUpUI()    }        func setUpUI() {                self.picImageView = UIImageView.init()        self.addSubview(self.picImageView!)                self.startImageView = UIImageView.init()        startImageView?.image = UIImage.init(named: "playBtn")        self.addSubview(self.startImageView!)                self.titleLabel = UILabel.init()        self.titleLabel?.textColor = UIColor.white;        self.titleLabel?.textAlignment = .center        self.addSubview(self.titleLabel!)                self.picImageView?.snp.makeConstraints { (make) in            make.top.equalTo(self).offset(0)            make.left.equalTo(self).offset(0)            make.width.equalTo(SCREEN_WIDTH)            make.height.equalTo(self)        }                self.startImageView?.snp.makeConstraints({ (make) in            make.center.equalTo(self.snp.center)            make.width.equalTo(100)            make.height.equalTo(100)        })                self.titleLabel?.snp.makeConstraints({ (make) in            make.top.equalTo((self.startImageView?.snp.bottom)!).offset(20)            make.width.equalTo(SCREEN_WIDTH)            make.height.equalTo(20)        })                            }        override func setSelected(_ selected: Bool, animated: Bool) {        super.setSelected(selected, animated: animated)        // Configure the view for the selected state    }}

 

控制器的代碼

import UIKitimport AVKitimport AVFoundationlet SCREEN_WIDTH = UIScreen.main.bounds.size.widthlet SCREEN_HEIGHT = UIScreen.main.bounds.size.heightclass ViewController : UIViewController, UITableViewDataSource,UITableViewDelegate {    lazy var tableView = UITableView()    let array:Array<Any> = []    var data = [                video(image: "videoScreenshot01", title: "Introduce 3DS Mario", source: "Youtube - 06:32"),        video(image: "videoScreenshot02", title: "Emoji Among Us", source: "Vimeo - 3:34"),        video(image: "videoScreenshot03", title: "Seals Documentary", source: "Vine - 00:06"),        video(image: "videoScreenshot04", title: "Adventure Time", source: "Youtube - 02:39"),        video(image: "videoScreenshot05", title: "Facebook HQ", source: "Facebook - 10:20"),        video(image: "videoScreenshot06", title: "Lijiang Lugu Lake", source: "Allen - 20:30")            ]        override func viewDidLoad() {        super.viewDidLoad()        self.tableView = UITableView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT), style: .plain)        self.tableView.dataSource = self        self.tableView.delegate = self        self.tableView.rowHeight = 220;        self.view.addSubview(tableView)           }        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        return data.count    }        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {        let customeIdntifier = "videoCell"        var cell = tableView.dequeueReusableCell(withIdentifier: customeIdntifier) as? VideoViewCell                        if cell == nil {            cell = VideoViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: customeIdntifier)        }                let video = data[indexPath.row]        cell?.titleLabel?.text = video.title        cell?.picImageView?.image = UIImage.init(named: video.image)        return cell!    }        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {        let playViewContoller = AVPlayerViewController()        var playerView = AVPlayer()                let path = Bundle.main.path(forResource: "emoji zone", ofType: "mp4")                playerView = AVPlayer(url: URL(fileURLWithPath: path!))                playViewContoller.player = playerView                self.present(playViewContoller, animated: true) {            playViewContoller.player?.play()        }                            }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.