Advertisement
Guest User

Swift button failure

a guest
Mar 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 7.79 KB | None | 0 0
  1. /
  2. //  ViewController.swift
  3. //  iHub
  4. //
  5. //  Created by Timothy Miller-Klugman on 3/11/17.
  6. //  Copyright © 2017 Timothy Miller-Klugman. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import AVFoundation
  11. import AVKit
  12.  
  13. let cellid = "cell"
  14.  
  15. class Post {
  16.     var videoName: String?
  17.     var views: String?
  18.     var videothumbnail: String?
  19.     var videourl: NSURL?
  20. }
  21. class VideoFeedController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
  22.  
  23.     var posts = [Post]()
  24.     var json: [Any]?
  25.    
  26.    
  27.     override func viewDidLoad() {
  28.         super.viewDidLoad()
  29.               let CatagoryMain = Post()
  30.        
  31.         posts.append(CatagoryMain)
  32.        
  33.         collectionView?.backgroundColor = UIColor(red:0.13, green:0.13, blue:0.13, alpha:1.0)
  34.         navigationItem.title  = "Main Video Feed"
  35.         collectionView?.alwaysBounceVertical = true
  36.         collectionView?.register(VideoFeedCell.self, forCellWithReuseIdentifier: cellid)
  37.    
  38.         let urlString = ""
  39.        
  40.         let url = URL(string: urlString)
  41.         URLSession.shared.dataTask(with:url!) { (data, response, error) in
  42.             if error != nil {
  43.                 print("failed")
  44.             } else {
  45.                 do {
  46.                    
  47.                     let parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
  48.                     let currentVideo = parsedData["video"] as! [String:Any]
  49.                    
  50.                    
  51.                     if let currentVideoTitle = currentVideo["title"] as? String {
  52.                         print(currentVideoTitle)
  53.                         CatagoryMain.videoName = "\(currentVideoTitle)"
  54.                         self.collectionView?.reloadData()
  55.        
  56.                     }
  57.                    
  58.                     if let currentViewCount = currentVideo["views"] as? String {
  59.                         CatagoryMain.views = "\(currentViewCount)"
  60.                          self.collectionView?.reloadData()
  61.                     }
  62.                    
  63.                     if let currentVideoThumbnail = currentVideo["thumb"] as? String {
  64.                         CatagoryMain.videothumbnail = "\(currentVideoThumbnail)"
  65.                         self.collectionView?.reloadData()
  66.                     }
  67.                    
  68.                     if let currentVideoURL = currentVideo["url"] as? String {
  69.                         CatagoryMain.videourl = NSURL(string: "\(currentVideoURL)")
  70.                         print(currentVideoURL)
  71.                         self.collectionView?.reloadData()
  72.  
  73.                     }
  74.  
  75.                 } catch let error as NSError {
  76.                     print(error)
  77.                 }
  78.             }
  79.            
  80.             }.resume()
  81.     }
  82.     override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  83.         return  posts.count
  84.     }
  85.    
  86.     override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  87.         let videoCell =  collectionView.dequeueReusableCell(withReuseIdentifier: cellid, for: indexPath) as! VideoFeedCell
  88.        
  89.         videoCell.post = posts[indexPath.item]
  90.         return videoCell
  91.    
  92.     }
  93.    
  94.     override func didReceiveMemoryWarning() {
  95.         super.didReceiveMemoryWarning()
  96.     }
  97.  
  98.     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  99.  
  100.        
  101.         return CGSize(width: 350, height: 150)
  102.     }
  103.  
  104.     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  105.                                //top //left //bottom //right
  106.         return UIEdgeInsetsMake(20, 0, 0, 0)
  107.     }
  108. }
  109.  
  110. class VideoFeedCell: UICollectionViewCell {
  111.    
  112.    
  113.     var post: Post? {
  114.         didSet {
  115.             if let name = post?.videoName {
  116.                
  117.                 let attributedText = NSMutableAttributedString(string: name, attributes: [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 14)])
  118.                
  119.                 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)]))
  120.                
  121.                 videoNameLabel.attributedText = attributedText
  122.             }
  123.            
  124.            
  125.         }
  126.        
  127.     }
  128.    
  129.    
  130.     override init(frame: CGRect) {
  131.         super.init(frame: frame)
  132.        
  133.         setupViews()
  134.     }
  135.    
  136.     required init?(coder aDecoder: NSCoder) {
  137.         fatalError("init(coder:) has not been implemented")
  138.     }
  139.    
  140.     let videoImageThumbnail: UIImageView = {
  141.          let thumbnail = UIImageView()
  142.          thumbnail.contentMode = .scaleAspectFit
  143.          thumbnail.backgroundColor = UIColor.red
  144.          thumbnail.translatesAutoresizingMaskIntoConstraints = false
  145.  
  146.         return thumbnail
  147.     }()
  148.    
  149.     let videoNameLabel: UILabel = {
  150.         let label = UILabel()
  151.         label.numberOfLines = 2
  152.         return label
  153.        
  154.     }()
  155.    
  156.     let playButton: UIButton = {
  157.         let button = UIButton()
  158.         let image = UIImage(named: "VideoIcon.png") as UIImage?
  159.         button.backgroundImage(for: .normal)
  160.         button.addTarget(self, action: #selector(pressBackButton(button:)), for: .touchUpInside)
  161.         button.setImage(image, for: .normal)
  162.        
  163.         return button
  164.     }()
  165.    
  166.     func pressBackButton(button: UIButton) {
  167.     print("test")
  168.         if let playVideoButtonURL = post?.videourl {
  169.            
  170.             let player = AVPlayer(url: playVideoButtonURL as URL)
  171.             let playerLayer = AVPlayerLayer(player:player)
  172.             playerLayer.frame = CGRect(x: 100, y: 200, width: 100, height: 100)
  173.             playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
  174.             self.layer.addSublayer(playerLayer)
  175.             player.play()
  176.            
  177.         }
  178.  
  179.     }
  180.    
  181.     func setupViews() {
  182.        
  183.         backgroundColor = UIColor(red:0.22, green:0.22, blue:0.23, alpha:1.0)
  184.        
  185.         self.layer.cornerRadius = 2
  186.         self.layer.shadowColor = UIColor(red:0.26, green:0.27, blue:0.27, alpha:1.0).cgColor
  187.         self.layer.shadowOffset = CGSize(width: 0, height: 1)
  188.         self.layer.shadowOpacity = 0.9
  189.         self.layer.shadowRadius = 5
  190.    
  191.         self.clipsToBounds = false
  192.         self.layer.masksToBounds = false
  193.        
  194.        
  195.         addSubview(videoNameLabel)
  196.         self.addSubview(playButton)
  197.         addSubview(videoImageThumbnail)
  198.                
  199.         // Horizontal
  200.         addConstraintsWithFormat(format: "H:|-8-[v0(44)]-8-[v1]|", view: videoImageThumbnail, videoNameLabel)
  201.         addConstraintsWithFormat(format: "H:|-280-[v0(44)]", view: playButton)
  202.        
  203.         // Vertical
  204.         addConstraintsWithFormat(format: "V:|-8-[v0]", view: videoNameLabel)
  205.         addConstraintsWithFormat(format: "V:|-8-[v0(44)]", view: videoImageThumbnail)
  206.         addConstraintsWithFormat(format: "V:|-90-[v0(44)]", view: playButton)
  207.     }
  208. }
  209.  
  210. extension UIView {
  211.    
  212.     func addConstraintsWithFormat(format: String, view: UIView...) {
  213.        
  214.         var viewDictionary = [String: UIView]()
  215.        
  216.         for (index, view) in view.enumerated() {
  217.             let key = "v\(index)"
  218.             viewDictionary[key] = view
  219.             view.translatesAutoresizingMaskIntoConstraints = false
  220.         }
  221.        
  222.         addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewDictionary))
  223.        
  224.     }
  225.    
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement