iphone开发:如何使用NSTimer创建动画
在我的应用程序中,我在视图中将显示此代码。它只是将随机对象从屏幕顶部到底部设置动画。问题是我需要检测碰撞,到目前为止我学到的是,不可能在任何时候检索动画的坐标。那么如何使用NSTimer实现它呢?还是我应该使用NSTimer?我想不出来。任何线索都将不胜感激iphone开发:如何使用NSTimer创建动画,iphone,objective-c,ios,animation,nstimer,Iphone,Objective C,Ios,Animation,Nstimer,在我的应用程序中,我在视图中将显示此代码。它只是将随机对象从屏幕顶部到底部设置动画。问题是我需要检测碰撞,到目前为止我学到的是,不可能在任何时候检索动画的坐标。那么如何使用NSTimer实现它呢?还是我应该使用NSTimer?我想不出来。任何线索都将不胜感激 -(void)viewWillAppear:(BOOL)animated { for (int i = 0; i < 100; i++) { p = arc4random_uniform(320)%4+1;
-(void)viewWillAppear:(BOOL)animated
{
for (int i = 0; i < 100; i++) {
p = arc4random_uniform(320)%4+1;
CGRect startFrame = CGRectMake(p*50, -50, 50, 50);
CGRect endFrame = CGRectMake(p*50, CGRectGetHeight(self.view.bounds) + 50,
50,
50);
animatedView = [[UIView alloc] initWithFrame:startFrame];
animatedView.backgroundColor = [UIColor redColor];
[self.view addSubview:animatedView];
[UIView animateWithDuration:2.f
delay:i * 0.5f
options:UIViewAnimationCurveLinear
animations:^{
animatedView.frame = endFrame;
} completion:^(BOOL finished) {
[animatedView removeFromSuperview];
}];
}
-(void)视图将显示:(BOOL)动画
{
对于(int i=0;i<100;i++){
p=Arc4随机均匀(320)%4+1;
CGRect startFrame=CGRectMake(p*50,-50,50,50);
CGRect endFrame=CGRectMake(p*50,CGRectGetHeight(self.view.bounds)+50,
50,
50);
animatedView=[[UIView alloc]initWithFrame:startFrame];
animatedView.backgroundColor=[UIColor redColor];
[self.view addSubview:animatedView];
[UIView animateWithDuration:2.f
延迟:i*0.5f
选项:UIViewAnimationCurveLinear
动画:^{
animatedView.frame=结束帧;
}完成:^(布尔完成){
[动画视图从SuperView移除];
}];
}
你可以让你的对象以小的增量(可能是循环)进行动画制作。每次你完成动画,你都可以得到坐标。我认为你可以在for循环中使用beginAnimations-committeanimations语法,它可以计算到10,所以在每个循环之后,你可以检查碰撞
CGRect incrementedViewFrame;
for(int i = 0; i < 10; ++i)
{
incrementedViewFrame = CGRectMake(/*calculate the coords here*/);
if(collision)
{
//do stuff
}
else
{
//do an other animation cycle
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.1f];
[[self viewToAnimate]setFrame:incrementedViewFrame];
[UIView commitAnimations];
}
}
CGRect增量视框;
对于(int i=0;i<10;++i)
{
incrementedViewFrame=CGRectMake(/*计算此处的坐标*/);
如果(碰撞)
{
//做事
}
其他的
{
//执行其他动画循环
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationBeginsFromCurrentState:是];
[UIView设置动画持续时间:0.1f];
[[self viewToAnimate]设置帧:递增的ViewFrame];
[UIView委员会];
}
}
我希望这会有所帮助!您的声明:
是否不可能在任何时候检索某个时间点的坐标
动画
似乎不正确。你检查过了吗
看起来您可以使用CALayer的presentationLayer
属性在动画期间提取坐标信息:
CGRect movingFrame = [[yourView.layer presentationLayer] frame];
我将使用此信息不时检查动画期间是否发生碰撞。因此,我将使用计时器检查碰撞状态,而不是为视图设置动画。我将放弃
NSTimer
,以便CADisplayLink
向目标发送消息。NSTimer
可能会导致动画口吃,因为它不会t与设备的屏幕刷新率同步。CADisplayLink
正是这样做的,并使您的动画像黄油一样运行
文件:
最终,使用CADisplayLink
看起来是这样的:
- (id) init {
// ...
CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update:)];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
// ...
}
- (void) update {
[myView updatePositionOrSomethingElse];
}
谢谢你的回答,但我真的不明白。你能再解释一点吗?当然可以,但告诉我你想检测的两个物体的碰撞。这真的很有用…非常感谢你出色的回答。