transitionWithView 不执行翻转动画

transitionWithView Not Performing Flip Animation

我正在使用以下代码尝试翻转 UIView。这是一张扑克牌。有一个容器视图,有两个 children,正面和背面。我在这里读过类似的问题,这应该是正确的方法,但实际的翻转本身并没有被执行。

我打算为此设置动画,将卡片移动到屏幕中央,然后将其从后向前翻转。向中心的移动很好,但从未发生翻转。

如果我将 View: 参数更改为 self,它会翻转整个视图控制器,如果我将它留在 cardContainer,它什么也不做。

不解:(

UIView *cardContainer = [[UIView alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height + 20, [Card size].width, [Card size].height)];
[self.view addSubview:cardContainer];

Card *card = _playerOneCards[_playerOneNextCardIndex];
card.frame = CGRectMake(0, 0, [Card size].width, [Card size].height);
card.delegate = self;
[cardContainer addSubview:card];

CardBack *back = [[CardBack alloc] initWithFrame:CGRectMake(0, 0, [Card size].width, [Card size].height)];
[cardContainer addSubview:back];


[UIView transitionWithView:cardContainer
                  duration:duration
                   options:UIViewAnimationOptionTransitionFlipFromLeft
                animations:^{
                    back.alpha = 0.0f;
                    cardContainer.center = self.center;
                }
                completion:nil];

试试这个。容器视图必须在动画开始之前放置在层次结构中。

//Add container views and subviews

[CATransaction flush];

[UIView transitionWithView:cardContainer
          duration:duration
           options:UIViewAnimationOptionTransitionFlipFromLeft
        animations:^{
            back.alpha = 0.0f;
            cardContainer.center = self.center;
        }
        completion:nil];

您还可以将函数包装在 dispatch_after 调用中。 有关更多信息,请查看:http://andrewmarinov.com/working-with-uiviews-transition-animations/