Add UIView to KeyWindow, How to Animate UIView Off Screen When pushViewController is Called?

472 views Asked by At

I have added a UIView to my [UIApplication sharedApplication].keyWindow and it is working properly.

When a button on the UIView is tapped, I want to push a new UIViewController onto the underlying NavigationController

This all works, but how can I make the UIView animate off the screen, to the left, with the underlying UIViewController ?

1

There are 1 answers

1
Achu22 On BEST ANSWER

You can animate your custom view and at the same time pass the control to the mail view controller to push the next view controller onto the navigation controller's stack... Below is the example code which I have simulated. The animation of your custom view should be in sync with the view controllers animation.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _v = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(0, 0, 50, 30)];
    [btn setTitle:@"Button" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_v addSubview:btn];
    _v.backgroundColor = [UIColor redColor];
    [[[UIApplication sharedApplication] keyWindow] addSubview:_v];

}

- (void)btnClicked:(id)sender {

    NSLog(@"Btn Clicked");

    [UIView animateWithDuration:0.2
                          delay:0.1
                        options: UIViewAnimationOptionCurveEaseOut
                     animations:^
     {
         _v.frame = CGRectMake(-_v.frame.size.width, 

_v.frame.origin.y, _v.frame.size.width, _v.frame.size.height);
         }
                         completion:^(BOOL finished)
         {
         }];
    UIViewController *c = [self.storyboard instantiateViewControllerWithIdentifier:@"TestVC"];
    [self.navigationController pushViewController:c animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}