2

In my didSelectRowAtIndexPath method I call [self dismissView]; when the user selects a cell in order to dismiss the view if its already been presented. This clearly isn't very optimal and its overriding the presentView method without it animating the dismissView. Is there a better way to do this? Or at the very least let it wait for the view to finish animating without using NSTimer.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    [self dismissView];

// Do xyz...

[self presentView:twitterLinksView];

Then..

- (void)presentView:(id)sender
{
    twitterLinksView.frame = CGRectMake(0, (self.view.frame.size.height + viewHeight), 320, 300);


    [UIView animateWithDuration:0.60f animations:^{


        CGRect twitterLinkFrame   = self.twitterLinksView.frame;
        twitterLinkFrame.origin.y = (self.view.frame.size.height - viewHeight);

        twitterLinksView.frame = twitterLinkFrame;
    }];
}

- (void)dismissView
{

    [UIView animateWithDuration:0.75f animations:^{

        CGRect twitterLinkFrame   = self.twitterLinksView.frame;
        twitterLinkFrame.origin.y = (self.view.frame.size.height + viewHeight);

        self.twitterLinksView.frame = twitterLinkFrame;
    }];
}
justMike
  • 235
  • 2
  • 9

2 Answers2

1
[UIView animateWithDuration:1. animations:^{

     //firstAnimationBlock

    } completion:^(BOOL finished){
        [UIView animateWithDuration:1. animations:^{

//animations in this block will be called after firstAnimationBlock has expired
        }];
    }];

as i understand you wanna fire 2 animation one after the other. this code (block inside block) makes this

this part is after edit: ok now you can try writing like that

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    [self dismissView];

// Do xyz...

[self performSelector:@selector(presentView:) withObject:twitterLinksView afterDelay:0.75];

//[self presentView:twitterLinksView];
}
meth
  • 1,887
  • 2
  • 18
  • 33
  • No not strictly speaking. As the first time the cell is selected and the view is animated up. The second time the cell is selected the view needs to animate down and then animate up again with new data. – justMike Mar 27 '13 at 22:00
  • ok. for your second option(first animate down and up again) you can use this block. first animatedown the page and then animate up again. for the first one, one block is enough unless i understand wrongly – meth Mar 27 '13 at 22:11
  • As per the code its two separate actions that should act independently, one does not follow the other. – justMike Mar 27 '13 at 23:21
0

I ended up splitting out my creation method and using [[self.view subviews] containsObject:twitterLinksView] to check for the view. Also to be noted is [self performSelector:@selector(createView) withObject:twitterLinksView afterDelay:0.76f] had to be very slightly ahead of the dismissing animation for it to work at all... yeah wtf.

In the didSelectRowAtIndexPath method i used [self performSelector:@selector(presentView:)];

Thanks to meth for solving part of the puzzle.

- (void)presentView:(id)sender
{

    if ([[self.view subviews] containsObject:twitterLinksView])
    {
        [self dismissView];
        [self performSelector:@selector(createView) withObject:twitterLinksView afterDelay:0.76f];
        NSLog(@"Animating ut old");

    }
    else
    {
        NSLog(@"Creating new view");
        [self createView];
    }
}
-(void)createView
{
    twitterLinksView.frame = CGRectMake(0, (self.view.frame.size.height + viewHeight), 320, 300);

    [UIView animateWithDuration:0.60f animations:^{

        CGRect twitterLinkFrame   = self.twitterLinksView.frame;
        twitterLinkFrame.origin.y = (self.view.frame.size.height - viewHeight);

        twitterLinksView.frame = twitterLinkFrame;
        [self.view addSubview:twitterLinksView];

    }];
}

- (void)dismissView
{

    [UIView animateWithDuration:0.75f animations:^{

        CGRect twitterLinkFrame   = self.twitterLinksView.frame;
        twitterLinkFrame.origin.y = (self.view.frame.size.height + viewHeight);

        self.twitterLinksView.frame = twitterLinkFrame;

    }completion:^(BOOL finished){
        [twitterLinksView removeFromSuperview];
    }];
}
justMike
  • 235
  • 2
  • 9