Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /
- // ViewController.swift
- // iHub
- //
- // Created by Timothy Miller-Klugman on 3/11/17.
- // Copyright © 2017 Timothy Miller-Klugman. All rights reserved.
- //
- import UIKit
- import AVFoundation
- import AVKit
- let cellid = "cell"
- class Post {
- var videoName: String?
- var views: String?
- var videothumbnail: String?
- var videourl: NSURL?
- }
- class VideoFeedController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
- var posts = [Post]()
- var json: [Any]?
- override func viewDidLoad() {
- super.viewDidLoad()
- let CatagoryMain = Post()
- posts.append(CatagoryMain)
- collectionView?.backgroundColor = UIColor(red:0.13, green:0.13, blue:0.13, alpha:1.0)
- navigationItem.title = "Main Video Feed"
- collectionView?.alwaysBounceVertical = true
- collectionView?.register(VideoFeedCell.self, forCellWithReuseIdentifier: cellid)
- let urlString = ""
- let url = URL(string: urlString)
- URLSession.shared.dataTask(with:url!) { (data, response, error) in
- if error != nil {
- print("failed")
- } else {
- do {
- let parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
- let currentVideo = parsedData["video"] as! [String:Any]
- if let currentVideoTitle = currentVideo["title"] as? String {
- print(currentVideoTitle)
- CatagoryMain.videoName = "\(currentVideoTitle)"
- self.collectionView?.reloadData()
- }
- if let currentViewCount = currentVideo["views"] as? String {
- CatagoryMain.views = "\(currentViewCount)"
- self.collectionView?.reloadData()
- }
- if let currentVideoThumbnail = currentVideo["thumb"] as? String {
- CatagoryMain.videothumbnail = "\(currentVideoThumbnail)"
- self.collectionView?.reloadData()
- }
- if let currentVideoURL = currentVideo["url"] as? String {
- CatagoryMain.videourl = NSURL(string: "\(currentVideoURL)")
- print(currentVideoURL)
- self.collectionView?.reloadData()
- }
- } catch let error as NSError {
- print(error)
- }
- }
- }.resume()
- }
- override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return posts.count
- }
- override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let videoCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellid, for: indexPath) as! VideoFeedCell
- videoCell.post = posts[indexPath.item]
- return videoCell
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
- return CGSize(width: 350, height: 150)
- }
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
- //top //left //bottom //right
- return UIEdgeInsetsMake(20, 0, 0, 0)
- }
- }
- class VideoFeedCell: UICollectionViewCell {
- var post: Post? {
- didSet {
- if let name = post?.videoName {
- let attributedText = NSMutableAttributedString(string: name, attributes: [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 14)])
- attributedText.append(NSAttributedString(string:"\n\(self.post?.views ?? "") Views", attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 12), NSForegroundColorAttributeName: UIColor(red: 155/255, green: 161/255, blue: 171/255, alpha: 1)]))
- videoNameLabel.attributedText = attributedText
- }
- }
- }
- override init(frame: CGRect) {
- super.init(frame: frame)
- setupViews()
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- let videoImageThumbnail: UIImageView = {
- let thumbnail = UIImageView()
- thumbnail.contentMode = .scaleAspectFit
- thumbnail.backgroundColor = UIColor.red
- thumbnail.translatesAutoresizingMaskIntoConstraints = false
- return thumbnail
- }()
- let videoNameLabel: UILabel = {
- let label = UILabel()
- label.numberOfLines = 2
- return label
- }()
- let playButton: UIButton = {
- let button = UIButton()
- let image = UIImage(named: "VideoIcon.png") as UIImage?
- button.backgroundImage(for: .normal)
- button.addTarget(self, action: #selector(pressBackButton(button:)), for: .touchUpInside)
- button.setImage(image, for: .normal)
- return button
- }()
- func pressBackButton(button: UIButton) {
- print("test")
- if let playVideoButtonURL = post?.videourl {
- let player = AVPlayer(url: playVideoButtonURL as URL)
- let playerLayer = AVPlayerLayer(player:player)
- playerLayer.frame = CGRect(x: 100, y: 200, width: 100, height: 100)
- playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
- self.layer.addSublayer(playerLayer)
- player.play()
- }
- }
- func setupViews() {
- backgroundColor = UIColor(red:0.22, green:0.22, blue:0.23, alpha:1.0)
- self.layer.cornerRadius = 2
- self.layer.shadowColor = UIColor(red:0.26, green:0.27, blue:0.27, alpha:1.0).cgColor
- self.layer.shadowOffset = CGSize(width: 0, height: 1)
- self.layer.shadowOpacity = 0.9
- self.layer.shadowRadius = 5
- self.clipsToBounds = false
- self.layer.masksToBounds = false
- addSubview(videoNameLabel)
- self.addSubview(playButton)
- addSubview(videoImageThumbnail)
- // Horizontal
- addConstraintsWithFormat(format: "H:|-8-[v0(44)]-8-[v1]|", view: videoImageThumbnail, videoNameLabel)
- addConstraintsWithFormat(format: "H:|-280-[v0(44)]", view: playButton)
- // Vertical
- addConstraintsWithFormat(format: "V:|-8-[v0]", view: videoNameLabel)
- addConstraintsWithFormat(format: "V:|-8-[v0(44)]", view: videoImageThumbnail)
- addConstraintsWithFormat(format: "V:|-90-[v0(44)]", view: playButton)
- }
- }
- extension UIView {
- func addConstraintsWithFormat(format: String, view: UIView...) {
- var viewDictionary = [String: UIView]()
- for (index, view) in view.enumerated() {
- let key = "v\(index)"
- viewDictionary[key] = view
- view.translatesAutoresizingMaskIntoConstraints = false
- }
- addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewDictionary))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement