gpt4 book ai didi

iOS:如何让后台线程在主线程上重绘 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:17:52 29 4
gpt4 key购买 nike

我的应用程序将查询数据库 50 次,然后通过将正确的 UIImageView 添加到另一个 UIImageView 做出相应的 react ,然后我希望应用程序在每个循环中立即显示它。

唉,经过很多个晚上我都没有让它工作。应用程序不会立即显示 UIImageView。话虽如此,当我在中间循环期间放大或缩小时,UIImageViews 将会出现!我一定是错过了什么......

到目前为止,除了最后一部分 [UIScrollView performSelector.... 之外,所有代码都有效。

请提前帮助并谢谢你。

    UIScrollView *firstView;
UIImageView *secondView;
UIImageView *thirdView;
NSOperationQueue *queue;
NSInvocationOperation *operation;

- (void)viewDidAppear:(BOOL)animated
{
queue = [NSOperationQueue new];
operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getData) object:nil];
[queue addOperation:operation];
}

- (void) getData
{
for (i=0 ; i < 50 ; i++)

{

//All the codes to facilitate XML parsing here
[nsXMLParser setDelegate:parser];
[BOOL success = [nsXMLParser parse];

if (success)
{

if ([parser.currentValue isEqualToString:@"G"])

thirdView.image = greenTick.jpg;
[secondView addSubview:thirdView];

}
else
{
NSLog(@"Error parsing document!");
}
}
[thirdView performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone: YES];
}

最佳答案

我发现了解决方案并真心希望这对某人有所帮助...

        if (success) 
{

if ([parser.currentValue isEqualToString:@"G"])

// Make the changes here - start
dispatch_queue_t main_queue = dispatch_get_main_queue();
dispatch_async(main_queue, ^{
thirdView.image = greenTick.jpg;
[secondView addSubview:thirdView];
});
// Make the changes here - end
}
else
{
NSLog(@"Error parsing document!");
}
}
// Remove performSelectorOnMainThread

关于iOS:如何让后台线程在主线程上重绘 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13502242/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com