从左向右移动图像

编程入门 行业动态 更新时间:2024-10-26 18:23:55
本文介绍了从左向右移动图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在视图加载后的应用程序中,图像应从左到右显示。 我在 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.

更多推荐

从左向右移动图像

本文发布于:2023-11-06 17:17:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564304.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!