简体   繁体   English

动画期间无法最小化主视图旁边的子视图

[英]Unable to minimize subview alongside mainView during animation

I have two views, videoView (which is the mainView) and subVideoView (which is the subView of mainView). 我有两个视图,videoView(这是mainView)和subVideoView(这是mainView的subView)。

I am trying to minimize both views using animation at the same time, as shown in the below code. 我试图同时使用动画最小化两个视图,如下面的代码所示。 I am able to minimize the videoView (ie mainView) and not the subVideoView. 我能够最小化videoView(即mainView)而不是subVideoView。

However, when I hide code for minimising videoView (ie mainView), I am able to minimise the subVideoView. 但是,当我隐藏用于最小化videoView(即mainView)的代码时,我能够最小化subVideoView。

I believe it has to do something with the way I am animating. 我认为它必须与我制作动画的方式有关。

Can some one please advice how I can minimise both views (proportionally) with animation at the same time and end up with the below result. 有人可以建议我如何同时用动画最小化(按比例)两个视图并得出以下结果。

在此处输入图片说明

RESULTS OF EXISTING CODE 现有代码的结果 在此处输入图片说明

func minimiseOrMaximiseViews(animationType: String){


        UIView.animate(withDuration: 0.5, delay: 0, options: [],

            animations: { [unowned self] in  
                switch animationType {
                case "minimiseView" :

                    // Minimising subVideoView

                    self.subVideoView.frame =   CGRect(x:       self.mainScreenWidth - self.minSubVideoViewWidth - self.padding,
                                                       y:       self.mainScreenHeight - self.minSubVideoViewHeight - self.padding,
                                                       width:   self.minSubVideoViewWidth,
                                                       height:  self.minSubVideoViewHeight)

                    // Minimising self i.e videoView

                    self.frame = CGRect(x:      self.mainScreenWidth - self.videoViewWidth - self.padding,
                                        y:      self.mainScreenHeight - self.videoViewHeight - self.padding,
                                        width:  self.videoViewWidth,
                                        height: self.videoViewHeight)

                    self.layoutIfNeeded()

                case "maximiseView":

                    // Maximising videoView

                    self.frame = CGRect(x: 0, y: 0, width: self.mainScreenSize.width, height: self.mainScreenSize.height)

                    // Maximising subVideoView

                    self.subVideoView.frame =   CGRect(x:       self.mainScreenWidth - self.maxSubVideoViewWidth - self.padding,
                                                       y:       self.mainScreenHeight - self.maxSubVideoViewHeight - self.padding - self.buttonStackViewBottomPadding - buttonStackViewHeight,
                                                       width:   self.maxSubVideoViewWidth,
                                                       height:  self.maxSubVideoViewHeight) 
                default:
                    break
}

As per your requirement add subViews in mainView and set 根据您的要求在mainView中添加subViews并设置

mainView.clipsToBounds = true

and In the minimiseOrMaximiseViews method you just need to manage Y axis of mainView for show and hide. minimiseOrMaximiseViews方法中,您只需要管理mainView的 Y即可进行显示和隐藏。

当我将两个视图(videoView和subVideoView)都设置为窗口的子视图(即UIApplication.shared.keywindow )时,可以同时使用动画最小化这两个视图。

Unless I'm missing something, this kind of animation is much more easily achieved by animating the transform property of the videoView (the main view). 除非我缺少任何东西,否则通过对videoView (主视图)的transform属性进行动画处理,可以更轻松地实现这种动画。 You will need to concatenate scaling and translation transforms for that, because scaling is by default applied to a view's center. 为此,您需要将缩放和平移转换串联起来,因为缩放默认情况下应用于视图的中心。

The trick is that applying a transform to a view affects all its subviews automatically, eg applying a scaling transform to a view scales both the view and all its subviews. 诀窍在于,将变换应用于视图会自动影响其所有子视图,例如,将缩放变换应用于视图会同时缩放视图及其所有子视图。

To make a reverse animation just set videoView.transform = CGAffineTransformIdentity inside an animation block. 要制作反向动画,只需在动画块内设置videoView.transform = CGAffineTransformIdentity

Caveat : You should be careful, though, with the frame property of a transformed view. 注意 :不过,使用转换视图的frame属性时要小心。 The frame property is synthetic and is derived from bounds , center and transform . frame属性是合成的,并且是从boundscentertransform派生的。 This means that setting a view's frame resets its transform property. 这意味着设置视图的frame重置其transform属性。 In other words, if you manipulate your views using transform , you most likely want to avoid setting their frames directly. 换句话说,如果使用transform操作视图,则很可能希望避免直接设置其框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM