AVPlayerViewController.ContentOverlayView中的自定义按钮 [英] Custom button in AVPlayerViewController.ContentOverlayView
问题描述
我需要为AVPlayerViewController添加一个自定义按钮,该按钮将在全屏和非全屏显示,适用于运行iOS 8的应用。
I need to add a custom button to the AVPlayerViewController that will appear in both fullscreen and non-fullscreen for an app running iOS 8.
添加按钮到AVPlayerViewController.view或包含视图将适用于非全屏,但当播放器切换到全屏时,按钮不再可见。我发现,如果我向AVPlayerViewController.ContentOverlayView添加一个按钮,那么它会以全屏和非全屏显示,但是然后看起来ContentOverlayView没有响应任何触摸,因此无法单击该按钮。有没有人知道添加按钮的不同地方或使ContentOverlayView响应触摸的方法?
Adding a button to the AVPlayerViewController.view or the containing view will work for non-fullscreen but when the player switches to fullscreen the button is no longer visible. I have found that if I add a button to the AVPlayerViewController.ContentOverlayView then it appears in fullscreen and non-fullscreen, but then it doesn't appear that the ContentOverlayView responds to any touches so the button cannot be clicked. Does anyone know of a different place to add the button or a way to make the ContentOverlayView respond to touches?
示例代码
AVPlayerViewController *playerView = [[AVPlayerViewController alloc] init];
playerView.player = [AVPlayer playerWithURL:movieURL];
CGRect viewInsetRect = CGRectInset ([self.view bounds],
kMovieViewOffsetX,
kMovieViewOffsetY );
/* Inset the movie frame in the parent view frame. */
[[playerView view] setFrame:viewInsetRect];
[self.view addSubview: [playerView view]];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor yellowColor];
btn.frame = CGRectMake(50, 50, 200, 75);
[btn addTarget:self action:@selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[btn setUserInteractionEnabled:YES];
[btn setEnabled:YES];
[playerView.contentOverlayView addSubview:btn];
推荐答案
我设法通过添加以下内容来实现此目的我在IB中的AVPlayerViewController:
I managed to get this working by adding the following to my AVPlayerViewController in IB:
- 里面有按钮的叠加视图
- 向中心添加约束叠加视图中的按钮
- 为视图创建了一个IBOutlet(我称之为'overlay')
- 从按钮创建一个动作到AVPlayerViewController打印的东西,以便我们可以看到点按按钮正在工作
- 将叠加视图拖出视图层次结构并拖到视图控制器的顶部栏上(显示为视图图标)视图控制器的顶部栏以及退出图标等)
- An overlay view with a button inside it
- Add constraints to center the button in the overlay view
- Created an IBOutlet to the view (I called it 'overlay')
- Created an action from the button to AVPlayerViewController that prints something so that we can see that tapping the button is working
- Dragged the overlay view out of the view hierarchy and onto the top bar of the view controller (appears as a view icon in top bar of the view controller along with the exit icon etc)
然后在AVPlayerViewController的代码中:
and then in the code for the AVPlayerViewController:
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.overlay)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.overlay.frame = self.view.bounds
}
@IBAction func playBtnTapped(sender: AnyObject) {
println("PlayBtnTapped")
}
此按钮现在以全屏/普通查看为中心,按钮正常工作
The button is now centered in fullscreen/normal viewing and the button works
这篇关于AVPlayerViewController.ContentOverlayView中的自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!