iPhone编程中UIImageView中渲染图像的问题

发布于 2024-08-30 16:30:11 字数 637 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

筱果果 2024-09-06 16:30:11

使用 NSTimer此处的文档)。定时器不会阻塞线程,而是返回到运行循环。这样 UIKit 可以提交视图层次结构中的更改并将其显示在屏幕上。在计时器的回调中,您只需更改图像即可。

Instead of sleep use an NSTimer (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.

柏林苍穹下 2024-09-06 16:30:11

.h

    @interface TwoImagesAppDelegate : NSObject <UIApplicationDelegate> {
        UIWindow *window;
        UIViewController* VC;
        UIImageView* imgView1;
        UIImageView* imgView2;
    }

.m

    - (void) toggle
    {
        static int toggle = 0;
        if (toggle) {
            imgView1.hidden = YES;
            imgView2.hidden = NO;
        } else {
            imgView1.hidden = NO;
            imgView2.hidden = YES;
        }
        toggle = toggle ? 0 : 1;
    }

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

        // Override point for customization after application launch

        [window makeKeyAndVisible];

        VC = [[UIViewController alloc]init];
        VC.view.backgroundColor = [UIColor redColor];
        imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,128,128)];
        [imgView1 setImage:[UIImage imageNamed:@"1.png"]];
        [VC.view addSubview:imgView1];
        [window addSubview:VC.view];

        VC.view.backgroundColor = [UIColor blackColor];
        imgView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,128,128)];
        [imgView2 setImage:[UIImage imageNamed:@"2.png"]];
        [VC.view addSubview:imgView2];  
        [window addSubview:VC.view];

        [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(toggle) userInfo:nil repeats:YES];

        return YES;
    }

请注意,此示例展示了如何使用定时器,存在内存泄漏。

.h

    @interface TwoImagesAppDelegate : NSObject <UIApplicationDelegate> {
        UIWindow *window;
        UIViewController* VC;
        UIImageView* imgView1;
        UIImageView* imgView2;
    }

.m

    - (void) toggle
    {
        static int toggle = 0;
        if (toggle) {
            imgView1.hidden = YES;
            imgView2.hidden = NO;
        } else {
            imgView1.hidden = NO;
            imgView2.hidden = YES;
        }
        toggle = toggle ? 0 : 1;
    }

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

        // Override point for customization after application launch

        [window makeKeyAndVisible];

        VC = [[UIViewController alloc]init];
        VC.view.backgroundColor = [UIColor redColor];
        imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,128,128)];
        [imgView1 setImage:[UIImage imageNamed:@"1.png"]];
        [VC.view addSubview:imgView1];
        [window addSubview:VC.view];

        VC.view.backgroundColor = [UIColor blackColor];
        imgView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,128,128)];
        [imgView2 setImage:[UIImage imageNamed:@"2.png"]];
        [VC.view addSubview:imgView2];  
        [window addSubview:VC.view];

        [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(toggle) userInfo:nil repeats:YES];

        return YES;
    }

please note that this example show how to use timer, there is memory leak.

默嘫て 2024-09-06 16:30:11

我假设你在窗户里。所以在

    @interface TwoImagesAppDelegate : NSObject <UIApplicationDelegate> {
        UIWindow *window;
        UIViewController* VC;
        UIImageView* imgView;
    }

.h.m

    - (void) addOneMore
    {
        VC.view.backgroundColor = [UIColor blackColor];
        [imgView setImage:[UIImage imageNamed:@"2.png"]];
        [VC.view addSubview:imgView];   
        [window addSubview:VC.view];
    }

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

        // Override point for customization after application launch

        [window makeKeyAndVisible];

        VC = [[UIViewController alloc]init];
        VC.view.backgroundColor = [UIColor redColor];
        imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,128,128)];
        [imgView setImage:[UIImage imageNamed:@"1.png"]];
        [VC.view addSubview:imgView];
        [window addSubview:VC.view];

        [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(addOneMore) userInfo:nil repeats:NO];

        return YES;
    }

I assume you are in a window. So in .h

    @interface TwoImagesAppDelegate : NSObject <UIApplicationDelegate> {
        UIWindow *window;
        UIViewController* VC;
        UIImageView* imgView;
    }

.m

    - (void) addOneMore
    {
        VC.view.backgroundColor = [UIColor blackColor];
        [imgView setImage:[UIImage imageNamed:@"2.png"]];
        [VC.view addSubview:imgView];   
        [window addSubview:VC.view];
    }

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

        // Override point for customization after application launch

        [window makeKeyAndVisible];

        VC = [[UIViewController alloc]init];
        VC.view.backgroundColor = [UIColor redColor];
        imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,128,128)];
        [imgView setImage:[UIImage imageNamed:@"1.png"]];
        [VC.view addSubview:imgView];
        [window addSubview:VC.view];

        [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(addOneMore) userInfo:nil repeats:NO];

        return YES;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文