Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show/hide a UIView with animation in iOS?

The main UIView contains two subviews - UIView_1 and UIView_2.
In the UIView_2, there is a button to show or hide the UIView_1.
For example, when a user touches the button to show the UIView_1, then UIView_1 will slide down and UIView_2 will push downwards with transition.
I have very little knowledge in animation. Can someone show me some sample code for reference?
Should I use CGAffineTransformMakeTranslation?
Thanks.enter image description here

like image 942
user403015 Avatar asked Dec 17 '11 04:12

user403015


People also ask

Does UIView animate need weak self?

You don't need to use [weak self] in static function UIView. animate() You need to use weak when retain cycle is possible and animations block is not retained by self.

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] . It's best to let the OS run your animations; the animation thread does not block the main thread -- only the animation block itself.


1 Answers

You don't need anything so complex. Just change the view's frame size.

    NSTimeInterval animationDuration = /* determine length of animation */;
    CGRect newFrameSize = /* determine what the frame size should be */;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    theViewToChange.frame = newFrameSize;
    [UIView commitAnimations];
like image 195
Shaggy Frog Avatar answered Sep 19 '22 06:09

Shaggy Frog