我在视图控制器中创建了一个tableview。我成功地隐藏并显示了表。但是,当它关闭并显示表时,它没有任何动画。我要它滑出来或滑进去。下面是我的代码。
@IBAction func clickButtonAction(sender: AnyObject) {
UIView.animateWithDuration(10.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
}) { (finished:Bool) -> Void in
if(self.tableView.hidden == true){
self.tableView?.hidden = false
}
else{
self.tableView.hidden = true
}
}
}
最佳答案
无法设置hidden
的UIView
属性的动画。您必须在0.0到1.0之间设置alpha
属性的动画。你也在完成你的动画。你应该在animations
区做。我也清理了一些代码。我假设tableView
声明为UITableView?
,但如果它是UITableView
或UITableView
,则可以在访问时省略?
:
@IBAction func clickButtonAction(sender: AnyObject) {
UIView.animateWithDuration(10.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
if self.tableView?.alpha == 1.0 {
self.tableView?.alpha = 0.0
}
else {
self.tableView?.alpha = 1.0
}
}, completion: { (finished:Bool) -> Void in
})
}
注意:您可能不想直接比较
alpha == 1.0
,因为floating point math is hard。相反,我建议保留一个局部变量,使用它来跟踪状态,然后将alpha
设置为关闭状态:// Make sure this matches the initial hidden state of your table view.
// So if your table view starts at alpha == 0.0, this should be true.
private var tableViewHidden: Bool = false
@IBAction func clickButtonAction(sender: AnyObject) {
tableViewHidden = !tableViewHidden
UIView.animateWithDuration(10.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
if tableViewHidden {
self.tableView?.alpha = 0.0
}
else {
self.tableView?.alpha = 1.0
}
}, completion: { (finished:Bool) -> Void in
})
}