简体   繁体   English

在UIViewController视图上对子视图进行动画处理

[英]Animate subview on UIViewController view

I have UITableView as a subview over UIView (parent view) of UIViewController. 我有UITableView作为UIViewController的UIView(父视图)的子视图。 When i'm animating my parent view as below: 当我为父视图设置动画时,如下所示:

       UIView.animateWithDuration(duration, animations: {
            viewController?.view.frame = CGRectMake(0,0,320,568)
        }, completion: {_ in

        })

I observed that my subview (table view) is not animating. 我观察到我的子视图(表格视图)没有动画。 I tried setting Autoresizing Mask of subviews to flexibleWidth and flexibleHeight, but din't get any success. 我尝试将子视图的自动调整蒙版设置为flexibleWidth和flexibleHeight,但没有成功。 Anybody having any idea why its happening. 任何人都不知道为什么会这样。

If you are using auto layout (which is the default in iOS 8/9 on Xcode 6/7) you need to change the constraints of your view inside he UIView animation closure, instead of changing the frame. 如果使用自动布局(这是Xcode 6/7上的iOS 8/9中的默认设置),则需要在UIView动画闭包内更改视图的约束,而不是更改框架。

If you are not using auto layout, then you can update the frame as above, however, I'd imagine you are. 如果您不使用自动版式,则可以如上所述更新框架,但是我想您是。 Thus you need to call layoutSubviews , then programatically set new constraints that represent your desired change to the layout, and finally call layoutSubviews again, inside the animation closure. 因此,您需要调用layoutSubviews ,然后以编程方式设置新约束来表示所需的布局更改,最后在动画闭包内再次调用layoutSubviews

Example: 例:

func AnimateBackgroundHeight() {
    self.view.layoutSubviews()
    UIView.animateWithDuration(0.5, animations: {
         self.heightCon.constant = 600 // heightCon is the IBOutlet to the constraint
         self.view.layoutSubviews()    
    })
}

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

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