iOS – 仅为特定视图翻转animation

我正在开发一个游戏,其中包含一些观点(作为记忆卡游戏),我希望当用户点击一张卡,这翻转,并显示另一种观点。 我使用这个代码:

- (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) { }]; } 

每个视图都有一个透明的button,覆盖整个视图,所以当用户点击一个视图是点击button。 button调用上面的方法传递发件人。 当animation开始时,所有视图都会翻转,不仅是发送者的视图。 我能怎么做?

以下代码可能有助于解决您的问题。 我认为它比使用透明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]; flipped = YES; } else { [frontCard setHidden:NO]; [backCard removeFromSuperview]; //or hide it. } } completion:nil]; } } 

我有同样的问题。 在互联网上search不同的职位后,我能够想出一个优雅和简单的解决scheme。 我有卡作为自定义UIButtons。 在自定义的UIButton类中,我添加了一个用翻转animation改变背景图像的方法:

 -(void) flipCard{ [UIView transitionWithView:self duration:0.3f options:UIViewAnimationOptionTransitionFlipFromRight|UIViewAnimationOptionCurveEaseInOut animations:^{ if (self.isFlipped) { [self setBackgroundImage:[UIImage imageNamed:@"card_back_2.png"] forState:UIControlStateNormal]; }else{ [self setBackgroundImage:[UIImage imageNamed:self.cardName] forState:UIControlStateNormal]; } } completion:NULL]; self.isFlipped = !self.isFlipped; } 

希望这可以帮助别人,因为第一个答案已经被接受了

UPDATE

如果你在包含这个子视图的视图上,代码是:

 -(void)flipCard:(APCard*)card{ [UIView transitionWithView:card duration:kFlipTime options:UIViewAnimationOptionTransitionFlipFromRight|UIViewAnimationOptionCurveEaseInOut animations:^{ if (card.isFlipped) { [card setBackgroundImage:[UIImage imageNamed:@"card_back_2.png"] forState:UIControlStateNormal]; }else{ [card setBackgroundImage:[UIImage imageNamed:card.cardName] forState:UIControlStateNormal]; } completion:^(BOOL finished) { if (finished) { //DO Stuff } } ]; card.isFlipped = !card.isFlipped; }