i have created tableview inside view controller. hidden , show table. however, doesn't have animation when closes , shows table. want slide out or slide in. following code.
@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 } } }
you can't animated hidden
property of uiview
. have animate alpha
property between 0.0 , 1.0 instead. you're doing work in completion block of animation. should doing in animations
block. cleaned code little bit. i'm assuming tableview
declared uitableview?
, if it's uitableview
or uitableview
, can omit ?
when accessing it:
@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 }) }
note: may not want directly compare alpha == 1.0
, since floating point math hard. instead, recommend keeping local variable, using track state, , base alpha
off of that:
// make sure matches initial hidden state of table view. // if table view starts @ alpha == 0.0, should 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 }) }