【发布时间】:2015-06-23 14:42:21
【问题描述】:
我正在尝试为我的应用程序中的 root-view-controller-change 设置动画。交换视图控制器后,我立即加载第二个控制器所需的数据。在加载数据时,我显示了一个加载器(MBProgressHUD)。这是我交换视图控制器的功能:
class ViewUtils {
class func animateRootViewController(duration: NSTimeInterval, changeToViewController: UIViewController) {
let window = UIApplication.sharedApplication().delegate?.window?
if window == nil {
return
}
UIView.transitionWithView(window!,
duration: duration,
options: UIViewAnimationOptions.TransitionFlipFromLeft | UIViewAnimationOptions.AllowAnimatedContent,
animations: {
window!.rootViewController = changeToViewController
},
completion: nil
)
}
}
这一切都很好,但有一件事 - 它完全破坏了装载机。我附上了正在发生的事情的想象: 这是旋转时的第二个视图控制器。旋转完成后,加载器看起来就好了,微调器和文本补间到圆角矩形中的正确位置。
我真的不明白为什么会发生这种情况,请有人向我解释一下吗?有什么办法可以预防吗?
我展示加载器的第二个视图控制器的代码:
override func viewDidLoad() {
super.viewDidLoad()
hud = HUD(containingView: view)
hud.show()
createBackground()
}
还有我的 hud 类:
class HUD {
private var hudBG: UIView!
private var view: UIView!
private(set) var isShown = false
init(containingView: UIView) {
view = containingView
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func show() {
if !isShown {
if(hudBG == nil) {
hudBG = UIView(frame: CGRectMake(0, 0, view.bounds.width, view.bounds.height))
hudBG.backgroundColor = UIColor(white: 0, alpha: 0.4)
}
view.addSubview(hudBG)
let hud = MBProgressHUD.showHUDAddedTo(view, animated: true)
hud.mode = MBProgressHUDModeIndeterminate
hud.labelText = "Cargando"
hudBG.alpha = 0
UIView.animateWithDuration(0.3, animations: { () -> Void in
self.hudBG.alpha = 1
})
isShown = true
}
}
func hide() {
if isShown {
UIView.animateWithDuration(0.3, animations: {
() -> Void in
self.hudBG.alpha = 0
}, completion: {
(b) -> Void in
self.hudBG.removeFromSuperview()
})
MBProgressHUD.hideHUDForView(view, animated: true)
isShown = false
}
}
}
非常感谢您的任何想法!
【问题讨论】:
标签: ios objective-c swift animation uiview