Pergunta

I want to move an image to a certain position. And I also need to check the current position of the image every second whether that image is reached in that certain position or not. So, I did the following:

-(void)viewWillAppear:(BOOL)animated{

 [self moshaMoveUp:mosha1 with:206 and:89];   
 playerLifeCount = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(deductPlayerLife) userInfo:nil repeats:YES];

}

-(void)moshaMoveUp: (UIImageView *)mosha with:(int)x and:(int)y{

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:7.0];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
mosha.center = CGPointMake(x, y);
[UIView commitAnimations];

}

But the problem is when the 'moshaMoveUp' method is called at the very beginning, the center of the image is set as (206,89) at that time. So, every second i check the current position, it shows me the result of the position I want the image to reach, not the current position. Even if the image is not yet reached in that position.

-(void)deductPlayerLife{

NSLog(@"x : %f",mosha1.frame.origin.x);
NSLog(@"y : %f",mosha1.frame.origin.y);

}

So, how can I get the real current position of that image every second without stopping the movement of the image.

Foi útil?

Solução

To get the current position of an animation, the QuartzCore presentationLayer can be used.

You would get mosha's position as follows:

CGRect moshaFrame = [[mosha1.layer presentationLayer] frame];

Please don't forget to import Quartzcore if you have not already done that.

#import <QuartzCore/QuartzCore.h>

P.S. I would use an animation block instead. The use of your animation style is discouraged since iOS 4.0.

Outras dicas

the ideal way to do this would be to use timer bound method call (such as update) move your image instead of UIView Animations.

interpolate x,y coordinates per timer call for your image and then in the same method (after interpolation) check the image's frame and its position to call deductPlayerLife.

as you are working on a game, I would suggest you to look for game development basics as well which would surely suggest this update thing.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top