我正在开发一个包含一些 View 的游戏(如存储卡游戏),我希望当用户点击卡片时翻转并显示另一个 View 。我使用这段代码:
- (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) {
}];
}
每个 View 都有一个覆盖整个 View 的透明按钮,因此当用户点击 View 时就像点击按钮一样。该按钮调用上面传递发件人的方法。 当动画开始时,所有 View 都会翻转,而不仅仅是我从发件人那里获得的 View 。 我该怎么办?
最佳答案
以下代码可能有助于解决您的问题。我认为它比使用透明按钮更干净。
- (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];
}
}
关于iOS - 仅针对特定 View 翻转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9032331/