UIImageViewer直到方法完成才更新其内容,而不是因为命令已执行 [英] UIImageViewer does't update its contents until methods finish, not as the commands are executed

查看:53
本文介绍了UIImageViewer直到方法完成才更新其内容,而不是因为命令已执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我的代码有问题.我想使用imageViewers制作某种类似于游戏的网格,但是当更改imageViewers的图片方式的方法时,它仅在完成执行后才更新屏幕/imageviewers,而不是在实际执行该行时才更新.我有一个带有无限循环的方法,当您按下按钮时,它会更改imageViewers的图片.这些按钮调用将布尔变量更改为true的方法,因此主游戏循环通过不断检查某些变量是否为真来知道按下了什么.但是因为游戏循环永远不会结束,所以屏幕永远不会更新.有没有一种方法可以在执行过程中更新屏幕上的内容?

Okay, I have a problem with my code. I want to make some sort of grid like game using imageViewers, but when the method that changes the pictures which the imageViewers how, it only updates the screen/imageviewers once it is finished executing, not when the line that does it is actually executed. I has a method with an infinite loop in it and it changes the pictures of the imageViewers as you press buttons. The buttons call methods which change a boolean variable to true, so the main game loop knows whats been pressed by constantly checking if certain variables are true. But because the game loop never ends the screen is never updated. Is there a way to update what's on the screen during execution?

我正在使用以下代码更改imageViewers的内容:

I am using this code to change the imageViewers contents:

     UIImage * white = [UIImage imageNamed: @"White.png"];
     UIImage * blue = [UIImage imageNamed: @"Blue.png"];
     int tagInt=1;
     for (int y = 0;y<10;y++){
         for (int x = 0;x<10;x++){
             UIImageView *imageView;
             imageView = [[UIImageView alloc] initWithImage:white];                 imageView.frame =CGRectMake(x*32,y*37,32,37);
             imageView.image = white;
             imageView.tag=tagInt;
             [self.view addSubview:imageView];
             tagInt++;
         }
     }
     for (int u = 1;u<20;u++){
         [NSThread sleepForTimeInterval:(1)];   
         UIImageView *g = (UIImageView*)[self.view viewWithTag:u];
         g.image =blue;
     }

这是一个人为的示例,因为我的实际代码太长,但是存在相同的问题.它只会在最后一个for循环(此延迟/线程睡眠命令)完成后才在屏幕上更新,而不是在代码行执行时更新.

This is a contrived example, because my actual code is too long, but has the same problem. It only updates on-screen after the last for loop (this delays/thread-sleep commands) finishes, not as the lines of code execute.

如果需要任何上下文,请参见以下项目: https://www.dropbox.com/s/cvefllnhgj0tp6g/Stacker.zip

Here is the project if you want any context: https://www.dropbox.com/s/cvefllnhgj0tp6g/Stacker.zip

推荐答案

在iOS中,所有与UI相关的工作都是在主(UI)线程中完成的.如果您有阻止ui线程的操作-这意味着,它使线程像您一样保持繁忙-不会发生视图更新.因此,您将必须以这种方式更改代码,即拥有一个"updateView"方法来更改图像视图的内容.必须定期调用此方法,例如通过使用NSTimer实例: https://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html

In iOS, all UI related stuff is done in the main (UI) thread. If you have an operation that blocks the ui thread - which means, it keeps the thread busy as you do - no view update will occur. So you will have to change your code that way, that you have a "updateView" method which changes the image views' content. This method will have to be called on a regular base, e.g. by using an NSTimer instance: https://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html

假设,用于更新视图的代码名为"updateView",则可以定义一个计时器,如下所示:

Assuming, the code to update your view is named "updateView", you could define a timer like this:

NSTimer *timer;
    timer = [NSTimer scheduledTimerWithTimeInterval: 1
                        target: self 
                        selector:@selector(updateView:) 
                        userInfo: nil 
                        repeats: YES];

因此,计时器将每秒调用一次updateView方法,并且不会阻塞主线程,这将再次使您对视图所做的更改变得可见.

So the timer would call your updateView method every second and would not block the main thread which again would allow the changes you made to the view to become visible.

这篇关于UIImageViewer直到方法完成才更新其内容,而不是因为命令已执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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