我有一个按钮,按下时会启动一个单独的线程来显示加载动画。这样做的原因是,按下按钮后立即显示加载gif,另一个过程继续,完成警报显示。我遇到的问题是在关闭警报后隐藏动画。
- (IBAction)buttonPressed:(id)sender {
[NSThread detachNewThreadSelector:@selector(loadAnimation) toTarget:self withObject:nil];
... do other things;
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Complete" message:@"other things done" delegate: self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert setTag:1];
[alert show];
}
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == 1) {
loadingGif.hidden=YES;
}
}
加载gif:
- (void) loadAnimation {
loadingGif.hidden=NO;
NSArray *imageArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"0.gif"], [UIImage imageNamed:@"1.gif"], [UIImage imageNamed:@"2.gif"], [UIImage imageNamed:@"3.gif"], nil];
loadingGif = [[UIImageView alloc] initWithFrame:CGRectMake(487, 520, 50, 50)];
[self.view addSubview:loadingGif];
loadingGif.animationImages = imageArray;
loadingGif.animationDuration = 1.5;
[loadingGif startAnimating];
}
动画加载正常,但一旦点击警报确定,它就不会停止。一旦在另一个线程中启动动画,是否可以隐藏动画?
答案 0 :(得分:1)
我不确定您是否应该在主线程以外的线程上进行UI更改。另一种方法是立即显示动画并使用NSTimer来安排其他不久之后执行的操作:
- (IBAction)buttonPressed:(id)sender {
// load animation on the main thread
[self loadAnimation];
// start a timer to do other stuff in 1 ms (will get executed on main thread)
[NSTimer scheduledTimerWithTimeInterval:0.001
target:self
selector:@selector(doOtherStuff)
userInfo:nil
repeats:NO];
}
- (void)doOtherStuff {
... do other things;
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Complete" message:@"other things done" delegate: self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert setTag:1];
[alert show];