获取CABasicAnimation的即时值 [英] Get instant value of a CABasicAnimation

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

问题描述

虽然我正在开发一款游戏,但我只是一个问题的主角,我认为这真的很容易,但是却不知道如何解决.如我所见,我在Objective-C中有点新:(

While i'm implementing a game, i'm just front of a matter, whose really easy i think, but don't know how to fix it. I'm a bit new in objective-c as you could see with my reputation :(

问题是,我有一个动画,可以正常工作.这是代码:

The problem is, i have an animation, which works correctly. Here is the code :

CABasicAnimation * bordgauche = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
bordgauche.fromValue = [NSNumber numberWithFloat:0.0f];
bordgauche.toValue = [NSNumber numberWithFloat:749.0f];
bordgauche.duration = t;
bordgauche.repeatCount = 1;
[ImageSuivante.layer addAnimation:bordgauche forKey:@"bordgauche"];

我想获取图像的当前位置.所以我用:

And i want to get the current position of my image. So i use :

CALayer *currentLayer = (CALayer *)[ImageSuivante.layer presentationLayer];
currentX = [(NSNumber *)[currentLayer valueForKeyPath:@"transform.translation.x"] floatValue];

但是我不能立即得到它.当我使用nslog打印它时,我得到了一次"Current = 0.0000",这是起始值,但之后没有得到. 我不知道如何一直获取图像的即时位置currentX.

But i don't get it instantly. I get one time "Current = 0.0000", which is the starting value, when i use a nslog to print it, but not the others after. I don't know how to get the instant position of my image, currentX, all the time.

我希望我表现不佳. 感谢您的帮助:)

I expect i was understable. Thanks for your help :)

推荐答案

我认为您在这里有3个选择(如果有更多选择,请发表评论):

I think you have 3 options here (pls comment if more exist):

选项1 :将您的第一个动画分成两部分,当上半部结束时开始播放该动画的下半部,再加上其他动画

option1: split your first animation into two and when the first half ends start the second half of the animation plus the other animation

...
bordgauche.toValue = [NSNumber numberWithFloat:749.0f / 2];
bordgauche.duration = t/2;
bordgauche.delegate = self // necessary to catch end of anim
[bordgauche setValue:@"bordgauche_1" forKey: @"animname"]; // to identify anim if more exist

...

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
    if ([theAnimation valueForKey: @"animname"]==@"bordgauche_1") {
         CABasicAnimation * bordgauche = [CABasicAnimation
                  animationWithKeyPath:@"transform.translation.x"];
         bordgauche.fromValue = [NSNumber numberWithFloat:749.0f / 2];
         bordgauche.toValue = [NSNumber numberWithFloat:749.0f];
         bordgauche.duration = t/2;
         bordgauche.repeatCount = 1;
         [ImageSuivante.layer addAnimation:bordgauche forKey:@"bordgauche_2"];


         // plus start your second anim 
}

选项2::设置NSTimer或CADisplayLink(这更好)回调,并连续检查动画层的参数.测试所需值的参数以触发第二个动画.

option2: setup a NSTimer or a CADisplayLink (this is better) callback and check continuously the parameters of your animating layer. Test the parameters for the required value to trigger the second anim.

displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(check_ca_anim)];
[displayLink setFrameInterval:1];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

... or
animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)(1.0 / 60.0) target:self selector:@selector(check_ca_anim) userInfo:nil repeats:TRUE];
[[NSRunLoop mainRunLoop] addTimer:animationTimer forMode:NSRunLoopCommonModes];

- (void) check_ca_anim {
    ...
    CGPoint currentPosition = [[ImageSuivante.layer presentationLayer] position];
    // test it and conditionally start something
    ...
}

选项3:设置CADisplayLink(现在称为游戏循环"),并通过计算和设置动画对象的适当参数来自己管理动画.不知道您想创建哪种游戏,我想说游戏循环可能由于其他特定于游戏的原因而有用.我在这里还提到了Cocos2d,它是游戏开发的绝佳框架.

option3: setup a CADisplayLink (can be called now as "gameloop") and manage the animation yourself by calculating and setting the proper parameters of the animating object. Not knowing what kind of game you would like to create I would say game loop might be useful for other game specific reasons. Also here I mention Cocos2d which is a great framework for game development.

这篇关于获取CABasicAnimation的即时值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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