简体   繁体   中英

How to animate transition between two views without using two view controllers?

I am trying to simulate a deck of cards, where a swipe over the screen makes the next card enter the display from the right, covering the card that is currently visible. I know how to program this using two view controllers and custom segues, but I want to keep it simple. I therefore want to stay within one and the same view controller, copy the current view into a temporary view, draw the new view, and then animate the new view to drift in from the right and cover the old one. Here's my attempt:

- (IBAction)SwipeLeft:(id)sender
{   UIView *sv = self.view;
    [self Layout]; // this is where the next card is drawn
    UIView *dv = self.view;

    dv.center = CGPointMake(sv.center.x + sv.frame.size.width, sv.center.y);
    sv.center = CGPointMake(sv.center.x, sv.center.y);

    [UIView animateWithDuration:0.3
                     animations:^
     {   dv.center = CGPointMake(sv.center.x, sv.center.y);
         sv.center = CGPointMake(sv.center.x - sv.frame.size.width, sv.center.y);
     }];

    NSLog(@"Swipe left");
}

When I run this code, I do see the new card enter the screen appropriately, however, the 'old' view dissappears immediately and turns into a black screen before the animation starts.

How can I correct this?

Clearly this code does not work, as both pointers point at the same memory location. I decided to solve my problem in a different way. Rather than trying to copy a View object, I create a screen shot, copy the UIImage into a View object, cover the screen under the copy of the old screen when I draw the new screen and than animate that View object. Probably a smarter solution. It works great.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM