iPhone编程中UIImageView中渲染图像的问题
我有一个 UIViewController 和一个 UIImageView,在 UIImageView 上我想在 2 个图像之间翻转,但我无法实现。
这是我写的代码,如果我错了请纠正我。
UIViewController* VC = [[UIViewController alloc]init];
VC.view.backgroundColor = [UIColor redColor];
UIImageView* imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320,400)];
[imgView setImage:[UIImage imageNamed:@"Image_A.jpg"]];
[VC.view addSubview:imgView];
sleep(2);
[imgView setImage:[UIImage imageNamed:@"Image_B.jpg"]];
[VC.view addSubview:imgView];
[window addSubview:VC.view];
因此,当我执行此项目时,屏幕上仅显示 Image_B,而我希望在睡眠(2)时显示 Image_A,则必须显示 Image_B。
我怎样才能使它成为可能?
谢谢。
I've a UIViewController and a UIImageView, on UIImageView i want to flip between 2 images, which i'm not able to achieve.
This is the code i've written, plz correct me if i'm wrong.
UIViewController* VC = [[UIViewController alloc]init];
VC.view.backgroundColor = [UIColor redColor];
UIImageView* imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320,400)];
[imgView setImage:[UIImage imageNamed:@"Image_A.jpg"]];
[VC.view addSubview:imgView];
sleep(2);
[imgView setImage:[UIImage imageNamed:@"Image_B.jpg"]];
[VC.view addSubview:imgView];
[window addSubview:VC.view];
so when i execute this project, only Image_B is displayed on screen, while i want Image_A to be displayed then on sleep(2), Image_B has to be displayed.
How would i make it possible??
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
NSTimer
(此处的文档)。定时器不会阻塞线程,而是返回到运行循环。这样 UIKit 可以提交视图层次结构中的更改并将其显示在屏幕上。在计时器的回调中,您只需更改图像即可。Instead of
sleep
use anNSTimer
(docs here). The timer does not block the thread, but instead returns to the run loop. This way UIKit can submit changes in the view hierarchy and display them on screen. In the callback of the timer you just change the image..h
.m
请注意,此示例展示了如何使用
定时器
,存在内存泄漏。.h
.m
please note that this example show how to use
timer
, there is memory leak.我假设你在窗户里。所以在
.h.m
I assume you are in a window. So in .h
.m