Dancer Dancer - 2 months ago 16
Objective-C Question

IOS 7 - Animate height of UiView

I have a simple overlay containing a white UI View - which in turn contains an activity indicator which is displayed whilst waiting for a server response.

When the server reply is complete I would like to animate the height of the uiView and display a hidden return button once the height animation is complete -

I've got the following so far (which Ive adapted from another post) - but I'm not sure if I'm on the correct track!?

- (void) showAnimation
{
[_overlayInner animateWithDuration:60
animations:^{
CGRect frame = left.frame;
// adjust size of frame to desired value
frame.size.height = 120;
left.frame = frame; // set frame on your view to the adjusted size
}
completion:^(BOOL finished){
[_rtnBtn setHidden:NO];
}];
}


Above shows an error for the first line stating ' no visible interface for uiview declares the selector animate with duration'. (_overlayInner is the view which I'd like to animate)

Am I barking up the wrong tree - or is there an easier way to animate uiview height?

Answer

That method is a class method on UIView.

You should be calling it like this...

[UIView animateWithDuration:60
             animations:^{
                 CGRect frame = left.frame;
                 // adjust size of frame to desired value
                 frame.size.height = 120;
                 left.frame = frame; // set frame on your view to the adjusted size
             }
             completion:^(BOOL finished){
                 [_rtnBtn setHidden:NO];
             }];

The documentation for this method.

You can see the declaration...

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

The + means it is a class method not an instance method.

EDIT

Just to explain this is how this works...

[UIView animateWithDuration:60 //this is the length of time the animation will take
             animations:^{
                 //this is where you change the stuff to its "final" state
             }
             completion:^(BOOL finished){
                 //this gets run after the animation is complete
             }];

So... if you have a view called myButton and its current frame is equal to CGRectMake(10, 10, 100, 44) and you would like to animate it 20 points to the right over 5 seconds and then log to the console that the animation has stopped then you could do...

[UIView animateWithDuration:5
             animations:^{
                 myButton.frame = CGRectMake(30, 10, 100, 44);
             }
             completion:^(BOOL finished){
                 NSLog(@"The animation has now stopped!");
             }];

If you want to double the height of the button over 15 seconds then you would do...

[UIView animateWithDuration:15
             animations:^{
                 myButton.frame = CGRectMake(10, 10, 100, 88);
             }
             completion:^(BOOL finished){
                 NSLog(@"The animation has now stopped!");
             }];

Just a quick note

Be careful if you are using AutoLayout and trying to animate stuff by changing the frames. They don't play nicely together. If you are just learning iOS and getting used to animating things then play around without using AutoLayout. Then you can venture into animating with AutoLayout later.