4

Depends upon the PlaybackControl Visibity How to show and hide the Custom Subview added in contentOverlayView?

I want to do like Youtube TVOS app did.

I tried with UIPressesEvent but it is not giving me the exact touch events. It is giving me these:

  override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
    for item in presses {
        switch item.type {
        case .Menu:
            self.customViews.alpha = 0
        case .PlayPause:
            self.player?.pause()
            self.customViews.alpha = 0
        default:
            self.setVisibilityToPreviewView()
        }
      }
    }

 func setVisibilityToPreviewView () { //This wont work in all cases.
      if self.previewView.alpha == 1 {
         self.previewView.alpha = 0
      } else {
         self.previewView.alpha = 1
      }
    }

But with this Touch events i can only show and hide the subviews. It should be hidden when the playbackcontrol is Hidden.

If I get the PlayBackControl Visibility values I don't need to worry about hiding these subviews.

Apple is Using AVNowPlayingPlaybackControlsViewController. It is not open for developers.

So I need to find some other better way to do this.

Please guide me how to do it.

Manikandan
  • 321
  • 2
  • 12

1 Answers1

4

You can register a tapGesture recognizer and then set its allowPressTypes property to UIPressType.Select, something like this

let tapRecognizer = UITapGestureRecognizer(target: self, action: "onSelect:")
tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)];
self.view.addGestureRecognizer(tapRecognizer)

And inside your action button show or hide custom overlays.

Example: Add this code inside a view controller and on tap (selecting at empty area on front of remote, touch area) you will see a message on console.

override func viewDidLoad() {
        super.viewDidLoad()
        let tapGesture = UITapGestureRecognizer(target: self, action: "onSelect")
        tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
        self.view.addGestureRecognizer(tapGesture);
    }
    func onSelect(){
        print("this is select gesture handler method");
    }

Update: Below is the code which will create AVPlayerController and will register tapGestureRecognizer to playervc.view.

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        playContent()
    }
    func playContent(){
        let urlString = "<contentURL>"
        guard let url = NSURL(string: urlString) else{
            return
        }
        let avPlayer = AVPlayer(URL: url)
        let playerVC = AVPlayerViewController()
        playerVC.player = avPlayer
        self.playerObj = playerVC

        let tapGesture = UITapGestureRecognizer(target: self, action: "onSelect")
        tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
        self.view.addGestureRecognizer(tapGesture);

        playerVC.view.addGestureRecognizer(tapGesture)
        self.presentViewController(playerVC, animated: true, completion: nil);

    }

Give a try to this, I think it should work fine.

Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54
  • This is for TVOS. And am using Remote control. In AVPlayerViewController how i can add this gesture. If i am adding this to self.view it is not working.. so did that to ContentOverlayview. that too not working – Manikandan Apr 22 '16 at 06:35
  • Yes I know that its TVOS and it should work. Add it to whatever view is on top. – Adnan Aftab Apr 22 '16 at 09:02
  • Thanks for your answer. But how to get visibility of PLAYBACKCONTROL in player. Need to check its visibility and hide the contents. – Manikandan Apr 24 '16 at 20:35
  • You can check yourCustomView.hidden value for any view to check if its hidden or visible. – Adnan Aftab Apr 25 '16 at 13:06
  • if PlaybackControl is hidden - Hide the subviews otherwise it should be visible. we can't go with yourCustomView.hidden value. Please check my updated Question. – Manikandan Apr 25 '16 at 13:26
  • There is no public API for that or Notification which will specifically tell you that you Playback Controls are now visible. I would suggest that you create some functionality by keep state of the player in context that when playback controls will be visible. Like touch, press & player states. – Adnan Aftab Apr 26 '16 at 08:06