0

I want to transit from UIView to another in the UIViewController when I press a button

I write this code:

- (IBAction)changeView:(id)sender {

    CATransition* transition = [CATransition animation];
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    transition.duration = 1.0f;
    transition.type =  @"cube";
    transition.subtype = @"fromTop";
    [self.view1.layer removeAllAnimations];
    [self.view1.layer addAnimation:transition forKey:kCATransition];


}

this code will transit the view1(UIview) to itself. how can I make it to transit to another UIView (e.g. view2). also where can I put the another UIview (view2) in the storyboard

Boss
  • 13
  • 5

3 Answers3

0

Have you try like this?

  self.view1.alpha = 0.0;

 [UIView beginAnimations:@"flip" context:nil];

 [UIView setAnimationDelegate:self];

 [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

 [UIView setAnimationDuration:1.0f];

  self.view2.alpha = 1.0;   

  [UIView commitAnimations];
wesley
  • 859
  • 5
  • 15
  • I want to use CATransition B/C it has Cube transit. FIY your code does not work for me :( – Boss Jul 10 '13 at 13:23
0

Have a look at this code snippet it's much more elegant. Also checkout Apple's documentation for more fancy UIViewAnimationOptions

- (IBAction)changeView:(id)sender
{
    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        //Remove View-1 from the main view
        [view1 removeFromSuperView];
        //Add the new view to your current view
        [self.view addSubView:view2];
    } completion:^(BOOL finished) {
        //Once animation is complete
    }];
}

Ensure that the two views are a strong property of your view controller.

vforvendetta
  • 596
  • 7
  • 15
  • I saw this code alot on the net but does not work for me. when I press the Button only view1 hide (removed)! – Boss Jul 10 '13 at 15:25
0

If you have two UIView for example take firstView and secondView and add it to UIViewController on same rect. and set hiddden property to secondView.

-(IBAction)flipFirstViewToSecondView{
secondView.hidden= NO;
    [UIView transitionWithView:firstView duration:1.0f options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{

        [UIView transitionWithView:secondView duration:1.0f options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{

        } completion:^(BOOL finished) {
            secondView.hidden = NO;
            firstView.hidden = YES;
        }];

    } completion:^(BOOL finished) {

    }];
}

Thanks. just let me know if you have any problem to integrate it.

SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39
  • Thanks alot for your help, but this don't look like a quality solution specially when I change the transit option. In addition, I want to use CATransition b/c it has more transit option e.g. cube transit. anyway thanks – Boss Jul 10 '13 at 15:14