iphone开发:使用NSTimer制作动画

iphone开发:使用NSTimer制作动画,iphone,ios,animation,imageview,nstimer,Iphone,Ios,Animation,Imageview,Nstimer,我编写了下面的代码,使用NSTimer将图像从顶部移动到按钮。但是,当我运行它时,我不能单独使用图像。基本上,图像留下了痕迹。我想要的是让图像一帧一帧地移动。我错过了什么 -(void)printOut:(NSTimer*)timer1; { image = [[UIImageView alloc] initWithFrame:CGRectMake(50, -50+m, 50, 50)]; [image setImage:[UIImage imageNamed:[NSStrin

我编写了下面的代码,使用NSTimer将图像从顶部移动到按钮。但是,当我运行它时,我不能单独使用图像。基本上,图像留下了痕迹。我想要的是让图像一帧一帧地移动。我错过了什么

-(void)printOut:(NSTimer*)timer1;
{

    image = [[UIImageView alloc] initWithFrame:CGRectMake(50, -50+m, 50, 50)];
    [image setImage:[UIImage imageNamed:[NSString stringWithFormat:@"image.png"]]];

    CGRect oldFrame = image.frame;
    image.frame = CGRectMake(oldFrame.origin.x+5, oldFrame.origin.y, oldFrame.size.width, oldFrame.size.height);

    [self.view addSubview:image];

    m++;
    if (m == 1000) {
        [timer1 invalidate];
    }

}
编辑:我不想使用动画。因为稍后我需要使用碰撞检测,并且不可能控制动画的坐标

这样做:

image.frame = (top frame here)

[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
//change time duration according to requirement
// animate it to the bottom of view
image.frame = (bottom frame here)
} completion:^(BOOL finished){
// if you want to do something once the animation finishes, put it here
}];
这样做:

image.frame = (top frame here)

[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
//change time duration according to requirement
// animate it to the bottom of view
image.frame = (bottom frame here)
} completion:^(BOOL finished){
// if you want to do something once the animation finishes, put it here
}];

您每次都在分配子视图并将其添加到self。何时删除ImageView以消失上一个ImageView。因此,请在分配之前尝试删除图像。像这样试试

-(void)printOut:(NSTimer*)timer1;
{
[image removeFromSuperview];
image = nil;
image = [[UIImageView alloc] initWithFrame:CGRectMake(50, -50+m, 50, 50)];
[image setImage:[UIImage imageNamed:[NSString stringWithFormat:@"image.png"]]];

CGRect oldFrame = image.frame;
image.frame = CGRectMake(oldFrame.origin.x+5, oldFrame.origin.y, oldFrame.size.width, oldFrame.size.height);

[self.view addSubview:image];

m++;
if (m == 1000) {
    [timer1 invalidate];
}
}

您每次都在分配子视图并将其添加到self。何时删除ImageView以消失上一个ImageView。因此,请在分配之前尝试删除图像。像这样试试

-(void)printOut:(NSTimer*)timer1;
{
[image removeFromSuperview];
image = nil;
image = [[UIImageView alloc] initWithFrame:CGRectMake(50, -50+m, 50, 50)];
[image setImage:[UIImage imageNamed:[NSString stringWithFormat:@"image.png"]]];

CGRect oldFrame = image.frame;
image.frame = CGRectMake(oldFrame.origin.x+5, oldFrame.origin.y, oldFrame.size.width, oldFrame.size.height);

[self.view addSubview:image];

m++;
if (m == 1000) {
    [timer1 invalidate];
}
}

我不能理解你的疑问,你能吗please@Rajneesh071嗯,当我运行这个代码时,图像从屏幕顶部出现并开始移动,但它不是逐帧发生的。我的意思是,当新帧出现时,前一帧不会消失。希望清楚:/UIImageView*image=[[UIImageView alloc]initWithFrame:CGRectMake(50,-50+m,50,50)];[image setImage:[UIImage ImageName:[NSString stringWithFormat:@“image.png”]];[self.view addSubview:image];我不能理解你的疑问,你能吗please@Rajneesh071嗯,当我运行这个代码时,图像从屏幕顶部出现并开始移动,但它不是逐帧发生的。我的意思是,当新帧出现时,前一帧不会消失。希望清楚:/UIImageView*image=[[UIImageView alloc]initWithFrame:CGRectMake(50,-50+m,50,50)];[image setImage:[UIImage ImageName:[NSString stringWithFormat:@“image.png”]];[self.view addSubview:image];我很抱歉,但问题是我不想使用动画。因为稍后我需要使用碰撞检测,并且不可能控制动画的坐标。哦,我很抱歉,但问题是我不想使用动画。因为稍后我需要使用碰撞检测,并且不可能控制动画的坐标。