Iphone 将图像从左向右移动

Iphone 将图像从左向右移动,iphone,objective-c,ipad,Iphone,Objective C,Ipad,在我的应用程序中,视图加载后,图像应该从左到右显示。 我正在viewdidload或viewwillbeen中编写此代码: UIImage *image = [UIImage imageNamed:@"PG05(REV).jpg"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; for (int i=-1024; i<=0; i++) {

在我的应用程序中,视图加载后,图像应该从左到右显示。 我正在
viewdidload
viewwillbeen
中编写此代码:

       UIImage *image = [UIImage imageNamed:@"PG05(REV).jpg"];
       UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
       for (int i=-1024; i<=0; i++) {
               NSLog(@"i: %d",i);
       sleep(5);
       imageView.frame = CGRectMake(i,0, 1024, 768);
               NSLog(@"done");
       [self.view addSubview:imageView];

       }
       [imageView release];
UIImage*image=[UIImage-imagename:@“PG05(REV.jpg)”;
UIImageView*imageView=[[UIImageView alloc]initWithImage:image];

对于(inti=-1024;i您可以使用函数并使用计时器方法调用它

timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(moveImage) userInfo:nil repeats:NO];

-(void)moveImage
{
//Your condition
}

首先,您不应该在for循环中调用-(void)addSubview:,只需调用一次即可。 第二,如果您想从左到右设置imageView的动画,那么可以使用块为此提供强大的功能

您的代码应该如下所示:

UIImage *image = [UIImage imageNamed:@"PG05(REV).jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(-1024, 0, 1024, 768);
[self.view addSubview:imageView];
[imageView release]; //Your imageView is now retained by self.view

//Animation
[UIView animateWithDuration:2.0
                 animations:^(void) {
                    imageView.frame = CGRectMake(0, 0, 1024, 768);
                 }];

animateWithDuration:方法将为您处理动画计时器,只需根据您的需要设置duration参数。

在循环之前,不应多次执行addSubView:操作。此外,您应该为此使用动画,而不是睡眠循环

例如:

imageView.frame = CGRectMake(-1024,0, 1024, 768);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 5];
imageView.frame = CGRectMake(0,0, 1024, 768);
[UIView commitAnimations];

这是UIView动画的作业

将imageView放在-1024左侧,然后转到:

[imageView animateWithDuration:5 animations:^{
    [imageView.frame = CGRectMake(0,0, 1024, 768)];
}];
Ta da!你的图像向右滑动的五秒钟动画

查看UIView的动画方法(UIImageView是其中的一个子类):

您好,非常感谢。请告诉我在触摸开始(触摸图像)时停止此动画的条件/代码。