ask:

a layers position is animating from one CGPoint to an other.

But I need to know the current position of the CALayer during the animation.

 

 

answer:

I tried for a while (I was trying to track the position to detect collisions between objects).
long story short: you can't (or uptil now nobody has been able to tell me how

I tried accessing the values during animations, putting observers etc etc

the problem is that you delegate the animation to Core Animation (I'm guessing) like in the move me example of apple.

then its CA that handels everything. it doesn't seem to physically move your layer. (in you code at the end you have to do something like whatIsAnimated.center = layerAnimated.center; (if you moving an object)

if your trying something simple like changing the animation while its executing then just end it and restart a new one

if your creating something much more precise like a game with moving object, collisions etc then you have to major choices:
- take out the big guns and use OpenGl ES
- create your own moveable object

both methods are similar (since the logic is the same) it really depends on what type of app you want and what are your overall goals

 

 

ask:

well I'm having a layer moving from A to B. Let's say this way: When the user taps the screen, a dot has to appear exactly there, where the layer is at this precise moment.
Yes it is a game.
But it's not worth OpenGL

 

 

 

answer:

I'm guessing you mean a dot appears where the layer is at the moment the screen is tapped 

well then if you want the cheap fix try something like this

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event{
animated.frame = [[animated.layer presentationLayer] frame];
[playerShip.layer removeAnimationForKey:@"animate"];
//display your point
//restartyour animation
}


cheap fix because you stop your animation to make your system update the position of the layer before restarting the animation
if you don't have a lot more animation then that should do the trick

 

 

Does this work for you or is there something I'm missing? For simplicity, the code is located in a view controller in a skeleton Xcode project--you can move it around according to your needs and do hit testing once you get the current position.

- (void)viewDidLoad
{
[super viewDidLoad];

layer = [CALayer layer]; // Declare layer as an ivar in your .h file
UIImage *image = [UIImage imageNamed:@"YourSpriteImage.png"];
layer.contents = (id)image.CGImage;
layer.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
[self.view.layer addSublayer:layer];

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(320, 480)];
anim.duration = 30.0;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[layer addAnimation:anim forKey:@"positionTest"];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
      CGPoint currentPosition = [[layer presentationLayer] position];
NSLog(@"%@", NSStringFromCGPoint(currentPosition));

     CALayer *layer=[[((UIImageView*)[_imagesInViewobjectAtIndex:0]).layerpresentationLayer]hitTest:point];

        if(layer)

       {

           [((UIImageView*)[_imagesInViewobjectAtIndex:0]).layer  removeAllAnimations];

       }

}

 

I'm pretty sure that somewhere down the line you are using a derivative of a UIView surely a UIImageVIew to display what your moving, and that the layer your refering to is the view layer that you get when doing something like this

- (void)animatePlacardViewToCenter{
CALayer *welcomeLayer = placardView.layer;
...

 

(code from the move me example of the apple website).

to build your own animation its pretty straight forwards (and a classic algorithm for game programing ^^)

create a loop that iterates at a high enough speed (human eye distinguishes around 23 FPS so you need at that, 3D engines to make clean animations work on anideal goal of 60FPS)

the basic physics : speed = distance / time

to create your animation you need to add a unit of movement usually referred as delta. at each iteration you add this delta to the position of the object you want to animate. the delta represents the distance your object has moved since the last iteration.

to go down this road (especially on a mobile platform) you have to optimize you code and limit the number of test you do.

thats the basics

 

 

answer:

Originally Posted by Kroupy
the only thing is...when the animation is done, it jumps back to the start....

This is because only the presentation layer changes from the animation. The model layer hasn't changed when the animation is done. Simply update your layer.position property when the animation is finished.