我正在尝试以动画方式增加 View 的宽度。它在第一个/基本 View Controller 中工作正常。但它在其他 View Controller 中不起作用。 首先,我将第二个 View Controller 加载为:
let vc = SecondViewController(nibName: "SecondViewController", bundle: nil)
self.presentViewController(vc, animated: true, completion: nil)
然后在它的 viewDidLoad 方法中我使用了:
UIView.animateWithDuration(10.0, delay:4, options: nil , animations: {
self.progressBarView.frame = CGRectMake(self.progressBarView.frame.origin.x, self.progressBarView.frame.origin.y, 367, self.progressBarView.frame.size.height)
}, completion: nil)
此动画在启动时加载的第一个 View Controller 中运行良好,但在第二个 View Controller 中不起作用,但 View 框架设置没有动画。看起来第二个 View Controller 中既没有工作延迟也没有动画持续时间。
最佳答案
viewDidLoad
在 View Controller 的 View 大小调整为当前显示大小之前被调用。如果您在 viewDidLoad 中查看 VC 的 contentView.frame,您会发现它是 XIB/Storyboard 中定义的大小,而不是正确的大小。
你应该把你的动画放在 viewWillAppear:
但是,viewWillAppear: 会在每次显示 VC 的 View 时调用,而不仅仅是第一次,因此您需要考虑到这一点。
另一个考虑因素:如果您使用的是自动版式,则不应操纵 View 框架。相反,您应该设置导出以约束这些 View ,并更改这些约束的值。
关于ios - 动画在某些 View Controller 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29448972/