-1

I'm trying to animate my view with the following code:

UIView.animate(withDuration: 1) { 
     fooView.isHidden = false
}

The problem is that the view's final position is almost on the bottom of the screen, and currently the animation is making it appear from the top of the screen and cross to the bottom.

I't like it appear from bottom, how can I control that? Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Addev
  • 31,819
  • 51
  • 183
  • 302
  • That code above moves the position of the `fooView`? You're only changing the visibility flag. – TawaNicolas Oct 31 '17 at 14:35
  • Possible duplicate of [UIStackView Hide View Animation](https://stackoverflow.com/questions/46326302/uistackview-hide-view-animation) – beyowulf Oct 31 '17 at 14:42
  • So, is there another animation that happens before this one which changes the view origin? – Ahmad F Oct 31 '17 at 14:43

2 Answers2

2

The isHidden UIView property is not animatable.

What you have to do is to set isHidden to false and the alpha to 0 before the animation begins, and animate the alpha property to a non-zero value. That will cause the view to fade in during the animation.

If isHidden is true for the duration of the animation then the view will be hidden for the whole duration.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
1

It seems like you want to unhide your view when it reaches its final position so you can use completion block of animation:

       UIView.animate(withDuration: 1, animations: { 
           //Animation
       }, completion: { (finished) in
              if finished {
                  fooView.isHidden = false  
              }
       })

Also, If you want to perform hide unhide with animation you can use it

      fooView.alpha = 0.0
      UIView.animate(withDuration: 1, animations: {
            fooView.alpha = 1.0
      }, completion: { (finished) in
            if finished {

            }
      })
Salman Ghumsani
  • 3,647
  • 2
  • 21
  • 34
  • The flag in the completion is not success, it's `finished`, this is to know if the animation was completed or may have been cancelled. Also, you're unhiding the view after the animation is completed, if it was hidden at the beginning of the animation, the alpha wouldn't be animated. – TawaNicolas Oct 31 '17 at 14:46
  • Why would you animate a view while it's in the hidden state, then set hidden to false in the completion handler? The effect would be a 1-second pause (while the view is hidden) and then the view would appear suddenly. – Duncan C Oct 31 '17 at 16:12
  • For your second block of code, assuming the view is hidden to start with. you'd need to add `fooView.isHidden = false` before the animation. – Duncan C Oct 31 '17 at 16:13
  • @DuncanC he did not post the whole code I give two solutions In first might be he wants to perform animation after that he wants to unhide the view as per his question. "The problem is that the view's final position....." And second I am trying to tell him how to hide with animation. that's the different thing from first answer. – Salman Ghumsani Oct 31 '17 at 16:18