我在视图控制器中创建了一个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
        }
    }

}

最佳答案

无法设置hiddenUIView属性的动画。您必须在0.0到1.0之间设置alpha属性的动画。你也在完成你的动画。你应该在animations区做。我也清理了一些代码。我假设tableView声明为UITableView?,但如果它是UITableViewUITableView,则可以在访问时省略?

@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

    })
}

10-05 20:21
查看更多