动画期间无法在 mainView 旁边最小化子视图 [英] Unable to minimize subview alongside mainView during animation

查看:11
本文介绍了动画期间无法在 mainView 旁边最小化子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图,videoView(主视图)和 subVideoView(主视图的子视图).

I have two views, videoView (which is the mainView) and subVideoView (which is the subView of mainView).

我正在尝试同时使用动画来最小化两个视图,如下面的代码所示.我能够最小化 videoView(即 mainView)而不是 subVideoView.

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 (i.e mainView) and not the subVideoView.

但是,当我隐藏用于最小化 videoView(即 mainView)的代码时,我能够最小化 subVideoView.

However, when I hide code for minimising videoView (i.e mainView), I am able to minimise the 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.

现有代码的结果

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
}

推荐答案

除非我遗漏了什么,否则通过为 transform 属性设置动画更容易实现这种动画videoView(主视图).您需要为此连接缩放和平移变换,因为缩放默认应用于视图的中心.

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). 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, e.g. applying a scaling transform to a view scales both the view and all its subviews.

要制作反向动画,只需在动画块内设置 videoView.transform = CGAffineTransformIdentity.

To make a reverse animation just set videoView.transform = CGAffineTransformIdentity inside an animation block.

警告:不过,您应该注意转换视图的 frame 属性.frame 属性是合成的,源自 boundscentertransform.这意味着设置视图的 frame 会重置其 transform 属性.换句话说,如果您使用 transform 操作您的视图,您很可能希望避免直接设置它们的框架.

Caveat: You should be careful, though, with the frame property of a transformed view. The frame property is synthetic and is derived from bounds, center and transform. This means that setting a view's frame resets its transform property. In other words, if you manipulate your views using transform, you most likely want to avoid setting their frames directly.

这篇关于动画期间无法在 mainView 旁边最小化子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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