Im made和DropDown Menu选择动作,如下面的GIF。Swift ||从Superview动画和删除子视图时出现Bug
所以我做了一个子视图和动画吧。 当动画子视图了,它看起来非常怪异。
特别是问题是,它看起来像一个空白的小酒吧,而不是菜单。
有谁知道问题出在哪里?
我做的子视图ViewController是一个简单的ViewController里面有一个TableView和一个原型单元格。
代码:
let blackView = UIView()
var tvx: OptionsVC = OptionsVC()
var h: CGFloat!
。
func optionsClicked() {
self.h = CGFloat(70 + (52 * (OptionsVC().arrayFunctionCellNames.count)))
navigationController?.hidesBarsOnTap = false
tvx = self.storyboard?.instantiateViewController(withIdentifier: "options") as! OptionsVC
if let window = UIApplication.shared.keyWindow {
blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)
blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))
view.addSubview(blackView)
view.addSubview(tvx.view)
let y = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.height
tvx.view.frame = CGRect(x: 0, y: (y - self.h), width: view.frame.width, height: h)
blackView.frame = CGRect(x: 0, y: y, width: view.frame.width, height: view.frame.height)
blackView.alpha = 0
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.blackView.alpha = 1
self.tvx.view.frame = CGRect(x: 0, y: y, width: self.view.frame.width, height: self.h)
self.blackView.frame = CGRect(x: 0, y: (y + self.h), width: self.view.frame.width, height: self.view.frame.height)
}, completion: nil)
}
}
。
func handleDismiss() {
UIView.animate(withDuration: 5.5, animations: {
let y = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.height
self.blackView.alpha = 0
if let window = UIApplication.shared.keyWindow {
self.blackView.frame = CGRect(x: 0, y: y, width: self.view.frame.width, height: self.view.frame.height)
self.tvx.view.frame = CGRect(x: 0, y: (y - self.h), width: self.view.frame.width, height: self.h)
}
}, completion: {(finished:Bool) in
self.blackView.removeFromSuperview()
self.tvx.view.removeFromSuperview()
self.navigationController?.hidesBarsOnTap = true
})
}
GIF的BUG:
我把它这么慢,所以你可以更好地看到它
只是一个评论通过。如果我要这样做,我只能使用约束,并将所述约束动画化为屏幕内外的动画,不能从超级视图中删除或更改帧。 – Glenn
我会同意@Glenn。你可以得到这个工作,但它不再是真正的标准。几年前,我做了这样的事情,它给了我同样的问题。如果我可以挖掘代码,我会与你分享。 –