iOS - Flip animation only for specific view

Andrea Mario Lufino picture Andrea Mario Lufino · Jan 27, 2012 · Viewed 28.7k times · Source

i'm developing a game which contained some view (as memory card game) and i want that when the user tap on a card this flip and shows another view. I use this code :

- (void)flipCard:(id)sender {

    UIButton *btn=(UIButton *)sender;
    UIView *view=[btn superview];
    UIView *flipView=[[UIView alloc] initWithFrame:[view frame]];
    [flipView setBackgroundColor:[UIColor blueColor]];
    [[flipView layer] setCornerRadius:10];

    NSLog(@"Flip card : view frame = %f, %f",view.frame.origin.x, view.frame.origin.y);

    [UIView transitionFromView:view toView:flipView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
    }];

}

Every view has a transparent button which cover the entire view, so when user tap on a view is as tap the button. The button call the method above passing the sender. When the animation starts all view is flipped, not only the view i get from sender. How can i do?

Answer

carlos picture carlos · Jan 30, 2012

The following code might help with your problem. I think it is cleaner than using a transparent button.

- (void)viewDidLoad {
    [super viewDidLoad];

    flipped = NO;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];

    [flipContainerView addGestureRecognizer:tapGesture];
    [tapGesture release];
}

- (void)handleTap:(UITapGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateEnded) {
        [UIView transitionWithView:flipContainerView
                          duration:1
                           options:UIViewAnimationOptionTransitionFlipFromLeft
                        animations:^{

            if (!flipped) {
                [frontCard setHidden:YES];
                [flipContainerView addSubview:backCard.view]; //or unhide it.
                flipped = YES;
            } else {
                [frontCard setHidden:NO];
                [backCard removeFromSuperview]; //or hide it.
            }

        } completion:nil];
    }
}