有时不出现动画 [英] Animation Sometimes Doesn't Occur

查看:97
本文介绍了有时不出现动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个专注的游戏。按下按钮即可翻转瓷砖。然后,我检查是否翻转了两个图块。当我不进行检查时,图块会按预期翻转。当我进行检查时,第一个图块会正确翻转。如果第二个图块与第一个图块匹配,则有时第二个图块在它们都消失后才进行翻转动画。

I am building a concentration-like game. The tiles get flipped by a button press. I then check to see if two tiles are flipped. When I don't do the check, the tiles flip as expected. When I do the check, the first tile flips correctly. If the second tile matches the first tile, sometimes the second tile doesn't do its flip animation before they both do their disappear animation.

-(void)twoTilesFlipped {

NSInteger i = 0;
for (i = 0; i < [bottomLayers count]; i++)
{
    if ([[flippedStatus objectAtIndex:i] isEqualToString: @"flipped"]) 
    {
        CALayer *tileOne = [bottomLayers objectAtIndex:i];
        NSInteger y = 0;
        for (y = 0; y < [bottomLayers count]; y++) 
        {
            if (y == i) 
            {
                continue;
            }

            if ([[flippedStatus objectAtIndex:y] isEqualToString: @"flipped"])

            {
                CALayer *tileTwo = [bottomLayers objectAtIndex:y];
                if (tileOne.contents == tileTwo.contents) {
                    _matchLabel.text = @"It's a match!";
                    [self disappearTheTiles:tileOne :tileTwo :i :y];
                    [self isGameOver];
                } else {
                    _matchLabel.text = @"Not a match!";
                    [self flipTheTilesBack:i :y];
                }
            }
        } return;

    }
}}

我是iOS编程的新手(和一般的编程(某种程度)。)

I am new to iOS programming (and programming in general (sort of)).

哦,这是翻转磁贴的方法:

Oh, here is the method for flipping the tiles:

- (void)flipTiles:(UIButton *)buttonPressed {
int b = buttonPressed.tag;

CALayer *top = [topLayers objectAtIndex:b];
CALayer *bot = [bottomLayers objectAtIndex:b];

if ([[flippedStatus objectAtIndex:b] isEqualToString: @"flipped"]) 
{
    top = [bottomLayers objectAtIndex:b];
    bot = [topLayers objectAtIndex:b];
}

CAAnimation *topAnimation = [self flipAnimationWithDuration:0.75f forLayerBeginningOnTop:YES scaleFactor:0.75f];
CAAnimation *bottomAnimation = [self flipAnimationWithDuration:0.75f forLayerBeginningOnTop:NO scaleFactor:0.75f];

CGFloat zDistance = 1500.0f;
CATransform3D perspective = CATransform3DIdentity; 
perspective.m34 = -1. / zDistance;
top.transform = perspective;
bot.transform = perspective;

topAnimation.delegate = self;
[CATransaction begin];
[top addAnimation:topAnimation forKey:@"buttonPressed"];
[bot addAnimation:bottomAnimation forKey:@"buttonPressed"];

[CATransaction commit];
if ([[flippedStatus objectAtIndex:b] isEqualToString: @"notFlipped"]) {
    [flippedStatus replaceObjectAtIndex:b withObject:[NSString stringWithFormat:@"flipped"]];
} else {
    [flippedStatus replaceObjectAtIndex:b withObject:[NSString stringWithFormat:@"notFlipped"]];
}
//NSLog(@"And if we got here, why didn't it flip? Huh?!");}

日志已打印,但有时磁贴在消失动画之前不会翻转。

The log prints, yet sometimes the tile doesn't flip before it does the disappear animation.

我是试图尽可能清晰,但我无法确定为什么有时而不是其他时候它会起作用。我认为这与 for循环和嵌套的 ifs有关,但是我不明白为什么动画没有发生,特别是因为NSLog总是出现在控制台中。

I am trying to be as clear as possible, but I can't get my mind around why it works sometimes and not other times. I think it has something to do with the "for" loops and the nested "ifs" but I don't understand why the animation doesn't happen especially since the NSLog always shows up in the console...

我在这里和其他站点上都看过很多与动画有关的问题-阅读书籍和教程中的许多动画部分,但到目前为止,没有什么可以作为答案。当然,如果有人需要查看,我可以提供整个项目...

I have looked at a lot of animation related questions here and on other sites - read a number of animation sections in books and tutorials, but so far, nothing leaps out as an answer. I can, of course, provide the entire project if anyone needs that to look at...

我尝试在战略性地点添加时间间隔,但无济于事。我还尝试跟踪磁贴的翻转状态,但这似乎不起作用。 -animationDidStop中是否有办法判断第二个图块是否已完成其翻转?我想我要问的是,有没有一种命名CAAnimation的方法(就像UIViewAnimation一样),这样我就可以知道特定的动画是否完成了? (是的-键值编码很好地解决了问题!)

I have tried adding time intervals in strategic places, to no avail. I have also tried tracking the flipped status of the tiles and that doesn't seem to work. Is there a way in -animationDidStop to tell if the second tile has completed its flip? I guess what I am asking is, is there a way to name a CAAnimation (like there is for a UIViewAnimation) so that I can tell if a specific animation completed? (Yes there is - key-value coding does the trick nicely!)

在Bill的帮助下,我现在开始为动画加标签。原来动画确实可以播放,但是动画消失后-这不是我所需要的。因此,在让消失动画发生之前,我需要进行某种检查以确保两次翻转均已发生。

With some help from Bill, I have now started key code tagging the animations. Turns out the the animation does play, but after the disappear animation - which isn't what I need to have happen. So I need to put in some sort of check to make sure both flips have happened before letting the disappear animation happen. Any thoughts appreciated!

添加更多跟踪之后,消失动画似乎在翻转动画之前发生-但仅在某些时候。对我来说,具有挑战性的部分是弄清楚如何告诉消失动画仅在两个翻转动画都发生之后才发生。努力吧!

After adding more tracking, it seems that the disappear animation is happening before the flip animation - but only sometimes. The challenging part for me is figuring out how to tell the disappear animation to only happen after both of the flip animations have occurred. Working on it! Suggestions still encouraged!

推荐答案

我不断尝试并最终做到了这一点:

I kept trying things and finally did this:

-(void)buttonPressed: (UIButton*)buttoni 
{
    [self flipTiles:(UIButton *)buttoni];
    [NSTimer scheduledTimerWithTimeInterval:2.0
                                     target:self
                                   selector:@selector(twoTilesFlipped)
                                   userInfo:nil
                                    repeats:NO];
}

我没有在其他地方弄糟,而是在这种方法中放置了计时器似乎有效。当然,其他一些操作现在不起作用,但是我认为这只是调整!谢谢!

Instead of mucking about other places, I put the timer in this method and it seems to work. Of course a few other things aren't working now, but I think that is just tweaking! Thank you!

这篇关于有时不出现动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆