从左向右移动图像 [英] Move image from left to right

查看:114
本文介绍了从左向右移动图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在视图加载后的应用程序中,图像应从左到右显示。
我在 viewdidload viewwillappear 中写这段代码:

In my application after the view load, an image should appear from left to right. I am writing this code in in viewdidload or viewwillappear:

       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];

但问题是,在加载视图之前它执行上面的代码而不是加载视图,所以它似乎加载了视图并且存在静态图像。
但我的要求是第一个视图加载完全然后图像应该从左到右显示。

But problem is that, before loading view it executes the above code than loads the view, so that it seems that view loaded and a static image is there. But my requirement is that first view load completely then image should display from left to right.

请帮帮我。

推荐答案

首先,你不应该在for循环中调用 - (void)addSubview:你只需要调用一次。
其次,如果您想从左到右为imageView设置动画, UIView类为使用块提供了很好的功能。

First of all, you should not call the -(void)addSubview: inside your for loop. You just need to call it once. Secondly, if you want to animate your imageView from left to right, UIView class provides great functionalities for this using blocks.

您的代码应如下所示:

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:方法将为您处理动画计时器,只需根据您的需要设置持续时间参数。

The animateWithDuration: method will handle the animation timer for you, just set the duration argument according to your needs.

这篇关于从左向右移动图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆