2017-08-13 23 views
0

目前我有我的代碼安裝到每個表格視圖單元格中有一個按鈕後,按下後​​顯示一個視頻。每個表格視圖單元格行都包含按鈕(有x個單元格),但無論按鈕在哪個行中點擊,總是會導致相同的視頻。有沒有辦法根據按鈕所在的行在哪裏顯示視頻?我的代碼目前只有一個視頻文件,但是如何根據點擊該按鈕的單元格將其顯示在哪裏,它會顯示特定的視頻?例如,如果在第一行中點擊按鈕,我希望它顯示某個視頻,而對於兩個和三個等等,則需要顯示相同的視頻。現在他們都顯示相同的視頻。如何讓按鈕根據按下的行顯示不同的視頻?

這裏是我的表視圖單元代碼:

import UIKit 
import AVFoundation 
import AVKit 

class VideoPlayerView: UIView { 

    let pauseButton: UIButton = { 
     let button = UIButton(type: .system) 
     button.setImage(#imageLiteral(resourceName: "Triangle 2"), for: .normal) 
     button.translatesAutoresizingMaskIntoConstraints = false 
     button.tintColor = UIColor.white 
     button.isHidden = false 
     button.addTarget(self, action: #selector(handlePause), for: .touchUpInside) 

     return button 
    }() 

    var player: AVPlayer? 
    var isPlaying = false 

    func handlePause() { 
     if isPlaying { 
      player?.pause() 
      pauseButton.alpha = 1.0 } 

     else { player?.play() 
      pauseButton.alpha = 0.01 
     } 

     isPlaying = !isPlaying 
    } 

    //container view that holds sublayers for the video control objects 
    let controlsContainerView: UIView = { 
     let view = UIView() 
     view.backgroundColor = UIColor(white: 0, alpha: 1.0) 
     return view 
    }() 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

     //setupPlayerView() 

     //configures container view (video's background) 
     controlsContainerView.frame = frame 
     addSubview(controlsContainerView) 
     backgroundColor = UIColor.black 


     //following adds pause/play button to video 
     controlsContainerView.addSubview(pauseButton) 
     pauseButton.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true 
     pauseButton.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true 
    } 

    //function that sets up video playback 
    private func setupPlayerView() { 

     //variable that contains video url 
     let fileUrl = URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/dunk.mov") 

     player = AVPlayer(url: fileUrl) 

     //video only renders if you specify 'playerLayer' 
     let playerLayer = AVPlayerLayer(player: player) 
     self.layer.insertSublayer(playerLayer, at: 1) 
     playerLayer.frame = frame 


     player?.play() 


     //attached obeserver of 'player' to tell when 'player' is ready 
     player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil) 

    } 

    //method called every time you add obserever to an object 
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { 
     //strring that lets AVPlayer know its ready 
     if keyPath == "currentItem.loadedTimeRanges" { 

      //configures container view while video is playing 
      controlsContainerView.backgroundColor = UIColor.clear 
      pauseButton.alpha = 0.05 
      isPlaying = true 

     } 
    } 
} 

class DrillsTableViewCell: UITableViewCell { 

    var videoURL:[URL] = [URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/dunk.mov"), URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/mk.MOV")] 
    var video = URL(fileURLWithPath: String()) 

    @IBOutlet weak var logoImage: UIImageView! 
    @IBOutlet weak var drillTitle: UILabel! 
    @IBOutlet weak var playButton: UIButton! 

    @IBAction func watchButton(_ sender: Any) { 
     print(123) 

     //controls video background view 
     if let keyWindow = UIApplication.shared.keyWindow { 
      let view = UIView(frame: keyWindow.frame) 
      view.backgroundColor = UIColor.white 

      view.frame = CGRect(x: 0.0, y: 0.0, width: keyWindow.frame.width, height: keyWindow.frame.height) 

      let videoPlayerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.width * 9/16) 
      let videoPlayerView = VideoPlayerView(frame: videoPlayerFrame) 
      view.addSubview(videoPlayerView) 

      keyWindow.addSubview(view) 
      UIView.animate(
       withDuration: 0.5, 
       delay: 0, 
       options: .curveEaseOut, 
       animations: { 
        view.frame = keyWindow.frame 

       }, 
       completion: { completedAnimation in 
        //possible features implemented later 
        UIApplication.shared.isStatusBarHidden = true 
       } 
      ) 
     } 
    } 
} 

代碼表視圖:

class DrillsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    var arrayForKey2 = [[String]]() 
    var keyIndex = Int() 
    var headLabel = String() 
    var labels = Array(trainingDict.keys) 

    @IBOutlet weak var tableView: DrillsTableView! 

    @IBOutlet weak var drillLabel: UILabel! 

    @IBOutlet weak var labelBackground: UIView! 


    @IBAction func back(_ sender: Any) { 
     performSegue(withIdentifier: "back", sender: self) 
    } 

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return arrayForKey2.count 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell" , for: indexPath) as! DrillsTableViewCell 
     cell.playButton.tag = indexPath.row 


     //clear background color needed in order to display gradient cell 
     cell.backgroundColor = UIColor.clear 

     //gradient configuration 
     gradient = CAGradientLayer() 
     gradient.frame = tableView.bounds 
     gradient.colors = [UIColor.black.cgColor, UIColor.darkGray.cgColor, UIColor.black.cgColor] 
     tableView.layer.insertSublayer(gradient, at: 0) 
     gradient.startPoint = CGPoint(x: 0.0, y: 0.0) 
     gradient.endPoint = CGPoint(x: 1.0, y: 1.0) 

     //Possible method for 'drillLabel' gradient 
     drillLabel.font = UIFont(name: "Symbol", size: 24.0) 

     //attributes for watch/play button 

     cell.playButton.layer.shadowColor = UIColor.black.cgColor 
     cell.playButton.layer.shadowOffset = CGSize(width: 2, height: 2) 
     cell.playButton.layer.shadowOpacity = 0.7 
     cell.playButton.layer.shadowRadius = 1 

     //details for cell label display 
     cell.borderWidth = 1.5 
     cell.borderColor = UIColor.white 
     cell.drillTitle.text = "\(arrayForKey2[keyIndex][indexPath.row])" 
     cell.drillTitle.font = UIFont(name: "Symbol", size: 18.0) 
     cell.drillTitle.textColor = UIColor.white 

     return cell 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.delegate = self 
     tableView.dataSource = self 
     drillLabel.text = labels[keyIndex] 
    } 
} 
+1

如果您沒有顯示相關的表格視圖代碼和按鈕處理代碼,沒有人能提供幫助。 – rmaddy

+1

這是「UIView」的代碼,而不是「UITableViewCell」 –

+0

我添加了認可代碼 –

回答

1

首先你要區分在細胞中添加的按鈕通過在tableView:cellForRowAtIndexPath中給它們添加標籤值:方法

cell.button.tag = indexpath.row 

這將爲每個單元格中存在的按鈕設置不同的標籤值。 然後在下面添加到它:

cell.button.addTarget(self, action:(YourController.buttonMethodPlayVideo(:)) , forControlEvents: .TouchUpInside) 

創建方法按鈕上點擊執行操作:

func buttonMethodPlayVideo(sender: UIButton) { 
    print(sender.tag) 
    } 

在上述方法時,你會點擊按鈕,你會得到不同的標記值。 根據該值,您可以播放不同的視頻或傳遞不同的視頻名稱給玩家玩vudeo。

+0

謝謝,還沒有實現代碼,但這是非常明確的,似乎適用!現在嘗試... –

+0

我已經添加了更多代碼來幫助更好地解釋我的問題。這是否改變了我的問題的解決方案? –

+0

歡迎@CaptainCode。 – Amit

2

我相信你應該重構你的代碼來獲得所需的行爲。請檢查下面的代碼:

首先要在VideoPlayerView方法改變命名setupPlayerView .Replace您實現這一點:

func setupPlayerView(for url: URL) { 




    player = AVPlayer(url: url) 

//video only renders if you specify 'playerLayer' 
    let playerLayer = AVPlayerLayer(player: player) 
    self.layer.insertSublayer(playerLayer, at: 1) 
    playerLayer.frame = frame 


    player?.play() 


    //attached obeserver of 'player' to tell when 'player' is ready 
    player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil) 

    } 

現在使DrillsTableViewCell變化,使videosURLs變化,我增加了一個新的變量singleVideoURL,新的類將是這樣的:

class DrillsTableViewCell: UITableViewCell { 

    var videoURLs:[URL] = [URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/dunk.mov"), URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/mk.MOV")] 




    @IBOutlet weak var logoImage: UIImageView! 

    @IBOutlet weak var drillTitle: UILabel! 


    @IBOutlet weak var playButton: UIButton! 
    @IBAction func watchButton(_ sender: UIButton) { 
print(123) 


    //controls video background view 
    if let keyWindow = UIApplication.shared.keyWindow { 
     let view = UIView(frame: keyWindow.frame) 
     view.backgroundColor = UIColor.white 
     var singleVideoURL = videoURLs[sender.tag] 

     view.frame = CGRect(x: 0.0, y: 0.0, width: keyWindow.frame.width, height: keyWindow.frame.height) 

     let videoPlayerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.width * 9/16) 
     let videoPlayerView = VideoPlayerView(frame: videoPlayerFrame) 
     videoPlayerView .setupPlayerView(for: singleVideoURL) 
     view.addSubview(videoPlayerView) 


     keyWindow.addSubview(view) 
     UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: { 

       view.frame = keyWindow.frame 

     }, completion: { (completedAnimation) in 
      //possible features implemented later 
      UIApplication.shared.isStatusBarHidden = true 


     }) 



} 
+0

這段代碼並沒有完成我想要做的事情。這很簡單。如果在第1行輕擊按鈕,我想要播放特定的視頻,第2行和第3行也是如此。按鈕應該知道它被按下的行,並且取決於它在特定視頻中的行(與該行相關聯)應該玩。 –

+0

@CaptainCode上面的代碼將從videoURLs數組中選擇url,比方說第0行中的按鈕被點擊,在你的tableview中,你將indexPath.row傳遞給button.tag,所以它將會是0,並且它將在action方法中出現,它會從videoURLs的0索引處選擇視頻網址並播放它,不是你想要的嗎?請糾正,如果我錯了 – 3stud1ant3

+0

是的,這是我想要完成的。 –

0

設置一個數組來把你的視頻的路徑進入,其indexe應該匹配單元的索引。另外,您可以將單元格的行號傳遞給內部按鈕作爲其標籤。所以當您點擊該按鈕時,您可以通過按鈕的標籤在陣列中找到特定的視頻路徑。

相關問題