iOS - 仅针对特定视图翻转动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9032331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:32:06  来源:igfitidea点击:

iOS - Flip animation only for specific view

iosanimationuiviewflip

提问by Andrea Mario Lufino

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?

每个视图都有一个覆盖整个视图的透明按钮,因此当用户点击视图时,就像点击按钮一样。按钮调用上面的方法传递发件人。当动画开始时,所有视图都被翻转,而不仅仅是我从发送者那里得到的视图。我能怎么做?

回答by carlos

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];
    }
}

回答by Sanandrea

I had the same problem. After searching different posts on the internet I was able to come up with an elegant and easy solution. I have the cards as custom UIButtons. In the custom UIButton class I added the method which changes the background image with a flip animation:

我有同样的问题。在互联网上搜索了不同的帖子后,我想出了一个优雅而简单的解决方案。我将卡片作为自定义 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;
}

Hope this helps someone else as the first answer has been already accepted

希望这对其他人有所帮助,因为第一个答案已被接受

UPDATE

更新

If you are on the view containing this sub-view the code is:

如果您在包含此子视图的视图上,代码为:

-(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;
}

回答by David DelMonte

My use case in Swift 4:

我在 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
            }
        })
    }