xcode 如何重绘 UIView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6829158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:34:21  来源:igfitidea点击:

How redraw UIView

iphoneobjective-cxcodecocoa-touch

提问by Artur Gurgul

In loadDidLoad controller call method with thread, like that:

在 loadDidLoad 控制器中使用线程调用方法,如下所示:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadImages) object:nil];
    [self.operationQueue addOperation:operation];
    [operation release]; 
}

loadImages method downloading image and show it:

loadImages 方法下载图像并显示它:

- (void) loadImages {

    @synchronized (self) {
        // download image...
        // ...
        // and next:
        UIImageView *imageView = [imageViews objectAtIndex:currentImageIndex];
        [imageView performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];
        [imageView performSelectorOnMainThread:@selector(setImage:) withObject:[UIImage imageWithData:imageData] waitUntilDone:YES];
    }
         //...
}

that work if show view first time, next time i must rotate iphone to show images. why view doesn't redraw?

如果第一次显示视图就可以工作,下次我必须旋转 iphone 才能显示图像。为什么视图不重绘?



i tried this code according to advices:

我根据建议尝试了此代码:

// imageButton is a UIView with button, image and label
NSData   *imageData = [[HttpClient sharedInstance] getResourceWithCache:thumbPath];
[imageButton performSelectorOnMainThread:@selector(setImage:) withObject:[UIImage imageWithData:imageData] waitUntilDone:YES];
[imageButton performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];
[imageButton drawRect:imageButton.frame];

but do not work.

但不工作。

回答by the.evangelist

your problem is in the line

你的问题是在线

        [imageView performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];

You would need to call setNeedsDisplay in self.view and not on self, as thats a method for UIView and not UIViewController.

您需要在 self.view 中调用 setNeedsDisplay 而不是在 self 上调用,因为这是 UIView 而不是 UIViewController 的方法。

Double check this by subclassing your view and putting a break point in drawRect: , as setNeedsDisplay must call drawRect.

通过子类化您的视图并在 drawRect: 中放置一个断点来仔细检查这一点,因为 setNeedsDisplay 必须调用 drawRect。

回答by Morten Fast

I'm assuming that loadDidLoadis a typo, and that you meant viewDidLoad.

我假设这loadDidLoad是一个错字,并且您的意思是viewDidLoad.

The viewDidLoadmethod is called when the view controller is done loading. It only does this once, unless the app receives a memory warning and the view controller gets unloaded.

viewDidLoad当视图控制器完成加载时调用该方法。它只执行一次,除非应用程序收到内存警告并且视图控制器被卸载。

If you're retaining the view controller and later showing the same view controller object again, the viewDidLoadmethod does not get called again.

如果您保留视图控制器并稍后再次显示相同的视图控制器对象,则viewDidLoad不会再次调用该方法。

If you need to perform an action every time the view controller gets pushed onto the view stack, you can use viewWillAppear:or viewDidAppear:.

如果每次将视图控制器推送到视图堆栈时都需要执行操作,则可以使用viewWillAppear:viewDidAppear: