Objective c 停止性能选择背景
在objective-c中,当按下按钮时,我加载处理动画,同时使用以下方式上载文件:Objective c 停止性能选择背景,objective-c,Objective C,在objective-c中,当按下按钮时,我加载处理动画,同时使用以下方式上载文件: [self performSelectorInBackground:@selector(loadAnimation) withObject:self]; 这将起作用并显示loadAnimation图像 文件上传后如何停止?我试过: [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(loadAnimation) o
[self performSelectorInBackground:@selector(loadAnimation) withObject:self];
这将起作用并显示loadAnimation图像
文件上传后如何停止?我试过:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(loadAnimation) object:nil];
但这并不会停止动画
加载动画是:
- (void) loadAnimation
{
loadingPng.hidden=NO;
NSArray *imageArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"0.png"], [UIImage imageNamed:@"1.png"], [UIImage imageNamed:@"2.png"], [UIImage imageNamed:@"3.png"], [UIImage imageNamed:@"4.png"], [UIImage imageNamed:@"5.png"], nil];
loadingPng = [[UIImageView alloc] initWithFrame:CGRectMake(487, 500, 50, 50)];
[self.view addSubview:loadingPng];
loadingPng.animationImages = imageArray;
loadingPng.animationDuration = 1.5;
[loadingPng startAnimating];
}
为了清楚起见,performSelectorInBackground创建了另一个线程,该线程执行该方法,如果没有连接到任何runloop,则该线程将终止
如果在执行过程中屏幕上出现了一些东西,并且您想要隐藏它,只需调用任何需要隐藏它的例程即可。如果使用UIActivityIndicatorView或UIImageView,则在本例中调用-voidstopAnimating[加载PNG停止动画]
没有必要在后台启动动画,实际上这是一件不应该做的事情,因为强烈建议只在主线程上执行任何涉及UI操作的操作
加载应转到后台,动画触发保留在主线程上。loadAnimation方法中到底发生了什么?如果调用了loadAnimation方法,则表示选择器已传递到目标-无需取消。列出的所有操作都相对较快,我认为这里不需要背景。