隐藏AVPlayerViewController中的控件 - 仅在开始时 [英] Hide controls in AVPlayerViewController -- only at start

查看:220
本文介绍了隐藏AVPlayerViewController中的控件 - 仅在开始时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果将AVPlayerViewController.showsPlaybackControls设置为false,则控件将根本不显示。即使你点击屏幕。

If you set AVPlayerViewController.showsPlaybackControls to false, the controls will not show at all. Even if you tap the screen.

我希望控件开始隐藏,但仍然能够通过点击召唤它们。如果我将提到的属性设置为true,它们就会开始显示。 (是的,它们会在几秒后消失。)有没有办法开始隐藏,但仍然可以访问?

I want the controls to start out hidden, but still be able to summon them by tapping. If I set the mentioned property to true, they start out visible. (Yes they fade after a few seconds.) Is there a way to start hidden, but still be accessible?

推荐答案

更新:我最终制作了自己的控件以便更好地进行自定义。这更难,但值得花时间。请阅读Apple的示例代码以供参考。它是关于实施PiP,也是关于制作自定义控件: https: //developer.apple.com/library/prerelease/ios/samplecode/AVFoundationPiPPlayer/Introduction/Intro.html

UPDATE: I ended up making my own controls for better customization. It's more difficult but worth the time. Please read Apple's sample code for reference. It's about implementing PiP but also about making custom controls: https://developer.apple.com/library/prerelease/ios/samplecode/AVFoundationPiPPlayer/Introduction/Intro.html

更新:点击时,AVPlayerViewController仅触发touchesBegan事件,而不触发事件。但它足以显示控件。

UPDATE: When tapped, AVPlayerViewController only fires touchesBegan event, and not touchesEnded event. But it's enough to show the controls.

首先,您需要隐藏控件。在呈现AVPlayerViewController之前放置此代码

First you need to hide the control. Put this code right before you present AVPlayerViewController

YourAVPlayerViewController.showsPlaybackControls = false

然后继承AVPlayerViewController并添加此函数:

Then subclass AVPlayerViewController and add this function:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    self.showsPlaybackControls = true

    super.touchesBegan(touches, withEvent: event)
}






旧的污染:


OLD SOLLUTION:

我刚刚解决了这个问题。主要的想法是将UIView放在AVPlayerViewController的顶部以显示轻敲手势,并在不再需要时隐藏UIView。

I've just solved this. The main idea is to put a UIView on top of the AVPlayerViewController to reveive tap gesture, and hide that UIView when it is no longer needed.

以下是代码:

import AVKit
import UIKit

// Create a custom AVPlayerViewController
@available(iOS 8.0, *)
final class CustomAVPlayerViewController: AVPlayerViewController {

    // Create a UIView to put on top of all
    lazy var topView = UIView(frame: CGRectMake(0, 0, width, height))

    override func viewDidLoad() {
        super.viewDidLoad()

        // For sure, set it to clearcolor
        // (DON'T set alpha = 0 because it will stop receiving user interaction)
        topView.backgroundColor = UIColor.clearColor()

        // Add it to the view of AVPlayerViewController
        self.view.addSubview(topView)

        // Bring it to front
        self.view.bringSubviewToFront(topView)

        // Add a tap gesture recognizer
        topView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap"))
    }

    // Handle the tap
    func handleTap() {

        // Show the control
        self.showsPlaybackControls = true

        // Hide the topView. You can unhide it when needed later.
        self.topView.hidden = true
    }
}

当你需要隐藏控件时,执行以下操作:

And when you need to hide the controls, do this:

var AVViewController = CustomAVPlayerViewController()

...

// Hide controls
AVViewController.showsPlaybackControls = false
// Show topView
AVViewController.topView.hidden = false

这篇关于隐藏AVPlayerViewController中的控件 - 仅在开始时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆