Search code examples
iosfacebookswiftavplayeravplayerlayer

ios: Move AVPlayerLayer to fullscreen


I have view hierarchy like following:

UIViewController
    UIView (Top most view)
        UIView (ContainerView)
            UIView (MoviePlayer.swift) (with layer as AVPlayerLayer)

The MoviePlayer file is posted here. Now I want to make the MoviePlayer view go full screen. The current code does not work good as the view is constrained inside another view. I tried making y co-ordinate negative but that does not help much. Somehow that approach also messes with the click events of buttons. Another approach I thought was to show a new view controller with the MoviePlayer view common between the two or something like that I don't know how to do that. I basically want to do that like the facebook iOS app shows in table view cells.

I don't want to use MPMoviePlayerController, it's already deprecated and had used it previously which had many problem and didn't allow customisations.

This is link to demo code on github where I tried a similar setup. I cannot share my real code. Clicking on fullscreen shows the problem. I actually have to also implement a similar video player in UITableViewCell.


Solution

  • You can try apply some CGAffineTransform to your layer.

    I face similar issue, and here is what work for me: I resize container with controls for player by changing it's constraint's constants and apply transform to player layer. Here example of method that create CGAffineTransform from CGRect.

    func CGAffineTransformFromRect(sourceRect: CGRect, toRect finalRect:CGRect) -> CGAffineTransform {
        var transform = CGAffineTransformIdentity
        transform = CGAffineTransformTranslate(transform, -(CGRectGetMidX(sourceRect) - CGRectGetMidX(finalRect)), -(CGRectGetMidY(sourceRect) - CGRectGetMidY(finalRect)))
        transform = CGAffineTransformScale(transform, finalRect.size.width / sourceRect.size.width, finalRect.size.height / sourceRect.size.height)
        return transform
    }