动画视频UIView像YouTube应用程序一样全屏

时间:2017-04-22 20:37:26

标签: swift animation uiview

我有一个名为UIView的视图控制器,我称之为playerView。在playerView中,我使用AVLayerAVPlayerLayer向用户显示视频。首次点击任何视频时,请在Youtube应用中进行成像。

如何操纵此playerView的框架,以便它可以占据整个屏幕。 (全屏)

这是当前的框架:

 //16 x 9 is the aspect ratio of all HD videos
 let height = view.frame.width * 9 / 16
 let playerFrame = CGRect(x: 0, y: 0, width: view.frame.width, height: height)    
 playerView.frame = videoPlayerFrame

我正在YouTube应用中进行测试,他们的应用只支持纵向导向(就像我的一样),因为视频的全屏动画看起来如何。如果用户水平握住设备,如何在按下按钮时进一步操作?

UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

   self.playerView.frame = ?

}, completion: nil)

编辑:试过这个,但它并没有真正起作用我想要它...它不占用整个屏幕

UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

   self.playerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.height, height: self.view.frame.width)
   self.playerView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))

}, completion: nil)

3 个答案:

答案 0 :(得分:3)

我明白了。

我创建了一个名为isExpanded的全局变量,当我们处于全屏模式时,该变量将设置为true。另外,我创建了一个名为videoPlayerViewCenter的CGPoint变量,因此我可以在全屏前保存中心,这样我就可以在返回小屏幕时再次设置它。

这是当用户想要全屏时调用的代码

func fullScreenTapped(sender: vVideoPlayer) {

        if !isExpanded {
            //Expand the video
            UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

                self.videoPlayerViewCenter = self.videoPlayerView.center

                self.videoPlayerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.height, height: self.view.frame.width)
                self.videoPlayerView.center = self.view.center
                self.videoPlayerView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
                self.videoPlayerView.layoutSubviews()

            }, completion: nil)
        } else {
            //Shrink the video again

            UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

                //16 x 9 is the aspect ratio of all HD videos

                self.videoPlayerView.transform = CGAffineTransform.identity
                self.videoPlayerView.center = self.videoPlayerViewCenter

                let height = self.view.frame.width * 9 / 16
                let videoPlayerFrame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: height)
                self.videoPlayerView.frame = videoPlayerFrame

                self.videoPlayerView.layoutSubviews()

            }, completion: nil)
        }

        isExpanded = !isExpanded
    }

要展开视频,我将中心保存在我的全局CGPoint变量中,然后设置框架。然后,我设置中心并使用CGAffineTransform进行90度转弯。

要再次缩小视频,我基本上将设置设置为之前的设置。 (完成任务的顺序非常重要)

另一件非常重要的事情是将videoPlayerView中的layoutSubviews方法覆盖为:

override func layoutSubviews() {
        super.layoutSubviews()
        self.playerLayer?.frame = self.bounds
    }

这将使你的playerLayer随着视图变大而调整其框架。

答案 1 :(得分:0)

正如我所说,这不是你问题的答案。这是一个想法,希望它可以帮助你:

 var rectangleView:UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Tap Gesture Recognizer for growing the rectangleView
    let tapForward = UITapGestureRecognizer.init(target: self, action: #selector(self.tapForward(tap:)))
    tapForward.numberOfTapsRequired = 1


    // Configure the rectangleView
    self.rectangleView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
    self.rectangleView.transform = CGAffineTransform.init(scaleX: 0.65, y: 0.25)
    self.rectangleView.backgroundColor = UIColor.red
    self.rectangleView.addGestureRecognizer(tapForward)
    self.view.addSubview(rectangleView)
}

这将是我们的tapForward(tap:)功能:

func tapForward(tap:UITapGestureRecognizer) {

    UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.8, options: .curveEaseInOut, animations: {

        self.rectangleView.transform = CGAffineTransform.identity

    }) { (completed:Bool) in
        // Completion block
    }
}

答案 2 :(得分:0)

我知道为时已晚,但也许其他人可以找到此代码有帮助。

我过去常常在视图中使用自动布局约束,因此动画设置必须采用其他方法。

func setMapFullScreen(_ fullscreen: Bool) {

    if fullscreen {
        // Set the full screen constraints
        self.setFullScreenLayoutConstraints()

        // Hide the navigation bar
        self.navigationController?.setNavigationBarHidden(true, animated: true)

    } else {
        // Set small screen constraints
        self.setSmallScreenLayoutConstraints()

        // Show the navigation bar
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }

    // Animate to full screen
    UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

        self.view.layoutIfNeeded()

    }) { (finished) in
        // ...
    }

在我的情况下,我有一个MKMapView,我想点击它以全屏查看地图(然后返回)。 这是状态的函数。

如您所见,在UIView.animate调用中,我仅对self.view.layoutIfNeeded()进行动画处理

我也隐藏了导航栏,因为我有一个☺

使用SnapKit框架在这里使用setSmallScreenLayoutConstraintssetFullScreenLayoutConstraints函数

func setSmallScreenLayoutConstraints() {
    self.mapView.snp.remakeConstraints { (make) -> Void in
        make.top.equalTo(self.mapLabel.snp.bottom).offset(8)
        make.left.equalTo(self.view).offset(0)
        make.right.equalTo(self.view).offset(0)
        make.height.equalTo(150)
    }
}

func setFullScreenLayoutConstraints() {
    self.mapView.snp.remakeConstraints { (make) -> Void in
        make.top.equalTo(self.view).offset(0)
        make.left.equalTo(self.view).offset(0)
        make.right.equalTo(self.view).offset(0)
        make.bottom.equalTo(self.view).offset(0)
    }
}

这是结果

Map animation

享受!