我已经以编程方式创建了UIButton
,并设置了当用户使用遥控器触摸该方法时要调用的方法。下面是我的代码。
import UIKit
class ViewController: UIViewController,VLCMediaPlayerDelegate,UICollectionViewDataSource,UICollectionViewDelegate {
@IBOutlet weak var btnClick: UIButton!
@IBOutlet weak var clickBtn: UIButton!
@IBOutlet weak var myLbl: UILabel!
@IBOutlet weak var playBtn: UIButton!
@IBOutlet weak var zoomBtn: UIButton!
@IBOutlet weak var logoutBtn: UIButton!
var movieView: UIView! // Add VLC Player on movieView
var mediaPlayer: VLCMediaPlayer = VLCMediaPlayer() // VLC Media Player
var PlayButton = UIButton()
var isZoom:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor( red: CGFloat(68/255.0), green: CGFloat(117/255.0), blue: CGFloat(196/255.0), alpha: CGFloat(1.0))
self.movieView = UIView()
self.movieView.backgroundColor = UIColor.black
self.movieView.frame = CGRect(x: self.view.frame.minX + 400, y: self.view.frame.minY, width: self.view.frame.size.width - 800, height: self.view.frame.height/2)
self.view.addSubview(self.movieView)
mediaPlayer.delegate = self
mediaPlayer.drawable = self.movieView
let url = "http://192.168.1.34:8852/out.ts"
let urls = URL(string: url)
let media = VLCMedia(url: urls!)
mediaPlayer.media = media
mediaPlayer.rate = 0
// mediaPlayer.play()
self.playButtonTapped(sender: mediaPlayer)
PlayButton = UIButton(type: .system)
PlayButton.setTitle("Play", for: .normal)
PlayButton.frame = CGRect(x: self.view.frame.midX - 100, y: self.view.frame.midY - 300, width: 200, height: 86) //midY-20
PlayButton.addTarget(self, action: #selector(pressButton), for: .touchUpInside)
// PlayButton.setImage(UIImage(named: "pause-button.png"/*"Pause-1.png"*/), for: .normal)
self.view.addSubview(PlayButton)
// self.view.bringSubview(toFront: PlayButton)
// self.view.sendSubview(toBack: mediaPlayer.drawable as! UIView)
}
//The target function
@objc func pressButton(){
print("Hello")
}
}
问题是pressButton
方法永远不会被调用。
答案 0 :(得分:0)
我发现问题出在tvOS上,是“ .touchUpInside”,因为这并没有达到我们的预期:我们需要使用“ .PrimaryActionTriggered”。
谢谢大家的支持。