我正在製作一個應用程序,顯示一個動畫UIImageView作爲指示應用程序繁忙的自定義方式。我正在使用NSOperationQueue進行文件上傳,我希望在隊列中有東西時顯示UIImageView。當隊列中的每個操作完成時,我想要刪除UIImageView。在NSOperationQueue完成後刪除動畫UIImageView
我認爲這是一件很容易做的事情,但我一直在過去的一個小時內被卡住了。顯示UIImageView非常簡單,但我似乎無法刪除它。這可能很簡單,我只是俯視。這是我的代碼。謝謝! :)
- (void)viewDidLoad {
//set up the uiimageview
self.spinnerView = [[UIImageView alloc] initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width-44,0,44,44)];
self.spinnerView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"0.gif"],
[UIImage imageNamed:@"1.gif"],
[UIImage imageNamed:@"2.gif"],
[UIImage imageNamed:@"3.gif"],
[UIImage imageNamed:@"4.gif"], nil];
self.spinnerView.animationDuration = 0.5f;
self.spinnerView.tag = 998;
self.spinnerView.animationRepeatCount = 0;
[self.view addSubview: self.spinnerView];
//set up the queue
self.uploadQueue = [[NSOperationQueue alloc] init];
[self.uploadQueue setMaxConcurrentOperationCount:1];
//set up observer for the queue
[self.uploadQueue addObserver:self forKeyPath:@"operationCount" options:NSKeyValueObservingOptionNew context:NULL];
}
- (void)newUpload:(NSData*)data {
[self.spinnerView startAnimating];
//....
//request is a NSURLRequest that's set up in this method
[NSURLConnection sendAsynchronousRequest:request queue:self.uploadQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
}];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context
{
if (object == self.uploadQueue && [keyPath isEqualToString:@"operationCount"]) {
if (self.uploadQueue.operationCount == 0) {
[self.spinnerView stopAnimating];
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object
change:change context:context];
}
我這樣做是否正確?有沒有更好的方法來做到這一點?我在這裏呆了一段時間,開始認爲這可能不是UIImageView的混亂,而是我將NSURLRequests添加到NSOperationQueue的方式。
再次感謝!
我沒有看到你的代碼中的任何地方,你刪除微調。如果你停止動畫,你應該有[self.spinnerView removeFromSuperview]; – rdelmar
你有沒有看到我的答案? –