user4691492
user4691492

Reputation:

How to add animation when the view becomes visible?

I have a UIView in a static UITableViewCell. The view is set to hidden in viewDidLoad. When a button gets selected, the view gets visible.

- (void)viewDidLoad {
    [self.myView setHidden:YES];
}

- (IBAction)myButton:(id)sender
{
    if (self.myView.hidden == YES) {
        [self.myView setHidden:NO];
    } else {
        [self.myView setHidden:YES];
    }

    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1)
    {
        if (self.myView.hidden == YES) {
            retuen 252 - self.myView.bounds.size.height;
        } else return 252;
    }
    tableView.estimatedRowHeight = 150;
    return UITableViewAutomaticDimention;
}

What happens now is, when the button is selected, the view becomes visible and the cells height changes with an animation; but the view gets visible right away, and it gets in the way of other objects (until the cells animation is over).

How can I make the view also enter with an animation? (I'd prefer it animate in height.)

Edit

I tried the following to animate the height, so that it should be synced with the cell

if (self.myView.hidden == YES) {
    [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, self.myView.frame.origin.y - self.myView.frame.size.height, self.myView.frame.size.width, self.myView.frame.size.height)];

    [self.myView setHidden:NO];
    [UIView animateWithDuration:1.0 animations:^(){
        [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, 0, self.myView.frame.size.width, self.myView.frame.size.height)];
    }];
}

But all it does is animates the views location, (from up to down,) not its height??

Upvotes: 1

Views: 555

Answers (5)

Jatin Patel - JP
Jatin Patel - JP

Reputation: 3733

@arnold, you are tried to change its Y Position, instead of height, you need to applied animation on its Height. like as belowed.

if (self.myView.hidden == YES) {
[self.myView setFrame:CGRectMake(self.myView.frame.origin.x, 0, self.myView.frame.size.width, 0)];

[self.myView setHidden:NO];
[UIView animateWithDuration:1.0 animations:^(){
    [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, 0, self.myView.frame.size.width, self.myView.frame.size.height)];
}];
}

Upvotes: 0

Sanjay Mohnani
Sanjay Mohnani

Reputation: 5967

Inside myButton: event you can use

- (IBAction)myButton:(id)sender
{

    if (self.myView.hidden == YES) {
        [self.myView setHidden:NO];
    } else {
        [self.myView setHidden:YES];
    }

   [self.myView setAlpha:0.0f];
   [UIView animateWithDuration:1.0
                          delay:0.0
                        options:nil
                     animations:^{
                         [self.myView setAlpha:1.0f];
                     }
                     completion:^(BOOL complete){
                         [self.myView setAlpha:1.0f];
                     }];

    [self.tableView beginUpdates];
    [self.tableView endUpdates];
} 

If you want to animate the height of the view, you can do as-

CGFloat viewHeight = self.myView.frame.size.height;
    [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, self.myView.frame.origin.y, self.myView.frame.size.width, 0)];
           [UIView animateWithDuration:1.0
                                  delay:0.0
                                options:nil
                             animations:^{
                                 [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, self.myView.frame.origin.y, self.myView.frame.size.width, viewHeight)];
                             }
                             completion:^(BOOL complete){
                                 [self.myView setFrame:CGRectMake(self.myView.frame.origin.x, self.myView.frame.origin.y, self.myView.frame.size.width, viewHeight)];
                             }];

Upvotes: 1

Mehul
Mehul

Reputation: 3178

//Show

   [UIView animateWithDuration:0.7f
                              delay:0
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             viewtemp.alpha = 0.912f;
                         } completion:NULL];

//hide

 [UIView animateWithDuration:0.9f
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         viewtemp.alpha = 0.0f;
                     } completion:NULL];

Upvotes: 0

Viral Savaj
Viral Savaj

Reputation: 3389

You can change it like this,

if (self.myView.hidden == YES) {
    [self.myView setAlpha:0.0];
    [self.myView setHidden:NO];
}
[UIView animateWithDuration:0.2 animations:^{
    if (self.myView.hidden == YES) {
         [self.myView setAlpha:1.0];
    } else {
         [self.myView setAlpha:0.0];
    }
}
completion:^(BOOL finished) {
    if (self.myView.alpha == 0.0) {
        [self.myView setHidden:YES];
    }
}];

May this helps you.

Enjoy Coding !!

Upvotes: 1

Fonix
Fonix

Reputation: 11597

the hidden property on a UIView is not animatable, you will need to unhide it, then set it to a state that it should be in as the animation is about to start (so maybe set alpha = 0, or height = 0) then in the animation block, set it to it correct state you want it to be in (alpha = 1, height = cell.frame.size.height)

eg

view.hidden = NO;
view.alpha = 0;

[UIView animateWithDuration:0.25 animations:^() {
    view.alpha = 1;
}];

Upvotes: 0

Related Questions