Ios 不停止UIView动画的图像的当前位置
我想将图像移动到某个位置。我还需要每秒检查一次图像的当前位置,看它是否到达了那个特定的位置。因此,我做了以下工作:Ios 不停止UIView动画的图像的当前位置,ios,iphone,objective-c,uiviewanimation,Ios,Iphone,Objective C,Uiviewanimation,我想将图像移动到某个位置。我还需要每秒检查一次图像的当前位置,看它是否到达了那个特定的位置。因此,我做了以下工作: -(void)viewWillAppear:(BOOL)animated{ [self moshaMoveUp:mosha1 with:206 and:89]; playerLifeCount = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(deductPlayerLi
-(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];
}
但问题是,当一开始调用“moshaMoveUp”方法时,图像的中心在当时设置为(206,89)。因此,每秒钟我检查一次当前位置,它就会显示我希望图像达到的位置的结果,而不是当前位置。即使图像尚未到达该位置
-(void)deductPlayerLife{
NSLog(@"x : %f",mosha1.frame.origin.x);
NSLog(@"y : %f",mosha1.frame.origin.y);
}
因此,如何在不停止图像移动的情况下每秒获取该图像的真实当前位置。实现这一点的理想方法是使用计时器绑定的方法调用(例如
update
)移动图像,而不是UIView动画
为图像的每个计时器调用插值x、y坐标,然后使用相同的方法(插值后)检查图像的帧及其位置以调用deductPlayerLife
当你在游戏中工作时,我建议你也去寻找游戏开发的基础知识,这肯定会给你带来
更新的建议。实现这一点的理想方法是使用限时方法调用(例如更新)来移动你的图像,而不是UIView动画
为图像的每个计时器调用插值x、y坐标,然后使用相同的方法(插值后)检查图像的帧及其位置以调用deductPlayerLife
当你正在制作一个游戏时,我建议你也去寻找游戏开发的基础知识,这肯定会建议你更新这个东西。要获得动画的当前位置,可以使用QuartzCorepresentationLayer
您将得到mosha的如下立场:
CGRect moshaFrame = [[mosha1.layer presentationLayer] frame];
如果尚未导入Quartzcore,请不要忘记导入
#import <QuartzCore/QuartzCore.h>
#导入
另外,我会用动画块来代替。由于iOS 4.0,不鼓励使用动画样式。要获取动画的当前位置,可以使用QuartzCorepresentationLayer
您将得到mosha的如下立场:
CGRect moshaFrame = [[mosha1.layer presentationLayer] frame];
如果尚未导入Quartzcore,请不要忘记导入
#import <QuartzCore/QuartzCore.h>
#导入
另外,我会用动画块来代替。由于iOS 4.0,不鼓励使用动画样式