我正在开发一款包含一些视图的游戏(作为存储卡游戏),我希望当用户点击卡片时这个翻转并显示另一个视图。我使用这段代码:
- (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) {
}];
}
每个视图都有一个覆盖整个视图的透明按钮,因此当用户点击视图时点击按钮。按钮调用上面的方法传递发件人。 当动画开始时,所有视图都被翻转,而不仅仅是我从发送者那里获得的视图。 我该怎么办?
答案 0 :(得分:46)
以下代码可能有助于解决您的问题。我认为它比使用透明按钮更清洁。
- (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];
}
}
答案 1 :(得分:13)
我遇到了同样的问题。在互联网上搜索不同的帖子后,我能够提出一个优雅而简单的解决方案。我有卡作为自定义UIButtons。在自定义UIButton类中,我添加了使用翻转动画更改背景图像的方法:
-(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;
}
希望这有助于其他人,因为第一个答案已被接受
<强>更新强>
如果您在包含此子视图的视图中,则代码为:
-(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;
}
答案 2 :(得分:0)
我在Swift 4中的用例:
@IBAction func flipCard() {
let transitionOptions: UIView.AnimationOptions = [.transitionFlipFromRight, .showHideTransitionViews]
UIView.transition(with: letterView, duration: 1.0, options: transitionOptions, animations: {
if self.showingBack == true {
self.letterImage.image = UIImage.init(named: self.letterImageName + ".png")
self.letterNameLabel.text = self.regularWord
self.showingBack = false
} else {
self.letterImage.image = UIImage.init(named: self.letterImageName + "C.png")
self.letterNameLabel.text = self.cursiveWord
self.showingBack = true
}
})
}