ios 加载新页面时动画微调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8990310/
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
Animate Spinner when loading new page
提问by Eli Stone
I have a UITableView which from an external RSS feed.
我有一个来自外部 RSS 提要的 UITableView。
When you select a row it uses navigationController and slides in from the right, the problem is that the RSS feed contains images therefore it can can take a few seconds to load and without any indication of what is going on you can mistake it for an application crash.
当您选择一行时,它使用 navigationController 并从右侧滑入,问题是 RSS 提要包含图像,因此加载可能需要几秒钟,并且没有任何迹象表明发生了什么,您可能会将其误认为是应用程序碰撞。
I decided to add a spinner so that you know that new page is loading.
我决定添加一个微调器,以便您知道正在加载新页面。
Here is my code:
这是我的代码:
RootViewController.m
根视图控制器.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Loading New Page");
[tableView deselectRowAtIndexPath:indexPath animated:YES];
DetailsViewController *detailViewController = [[DetailsViewController alloc] initWithNibName:@"DetailsViewController" bundle:nil];
detailViewController.item = [rssItems objectAtIndex:floor(indexPath.row/2)];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGPointMake(160, 240);
[self.view addSubview:spinner];
[spinner startAnimating];
[spinner release];
}
DetailsViewController.m
细节视图控制器.m
- (void)viewDidLoad {
[super viewDidLoad];
NSString *imgURL = [item objectForKey:@"image"];
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imgURL]];
item_photo.image = [[UIImage alloc] initWithData:mydata];
item_title.text = [item objectForKey:@"title"];
item_date.text = [NSString stringWithFormat:@"Date: %@",[item objectForKey:@"date"]];
item_time.text = [NSString stringWithFormat:@"Time: %@",[item objectForKey:@"time"]];
item_cost.text = [NSString stringWithFormat:@"Cost: £%@",[item objectForKey:@"cost"]];
item_info.text = [item objectForKey:@"description"];
self.navigationItem.title = @"Event Type";
}
There are two problems with this code.
这段代码有两个问题。
- The Spinner does not active until after the new page has loaded.
- The Spinner does not disable once loaded.
- Spinner 在新页面加载后才会激活。
- Spinner 在加载后不会禁用。
If anyone could help me with this problem i would be truly gratefully.
如果有人能帮助我解决这个问题,我将不胜感激。
回答by wattson12
You are adding the activity indicator view to the view of the controller which is pushing the detail view controller, so you wont see it anyway
您正在将活动指示器视图添加到推送详细视图控制器的控制器视图中,因此无论如何您都不会看到它
try moving the second group of code to the viewDidLoad method of DetailsViewController, you can call stopAnimating on the activity indicator when you are finished loading. To get a reference to the UIActivityIndicator you should add a tag
尝试将第二组代码移到DetailsViewController的viewDidLoad方法中,可以在加载完成后调用活动指示器上的stopAnimating。要获得对 UIActivityIndicator 的引用,您应该添加一个标签
e.g. in viewDidLoad
例如在 viewDidLoad
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGPointMake(160, 240);
spinner.tag = 12;
[self.view addSubview:spinner];
[spinner startAnimating];
[spinner release];
in the loadingFinished method (whichever method is called when finished loading)
在 loadingFinished 方法中(完成加载时调用的方法)
[[self.view viewWithTag:12] stopAnimating];
回答by ikuramedia
You need to do some work in a background thread. If the following line is the one that takes the time:
您需要在后台线程中做一些工作。如果以下行是需要时间的行:
detailViewController.item = [rssItems objectAtIndex:floor(indexPath.row/2)];
Then you could do this in the background with GCD:
然后你可以使用 GCD 在后台执行此操作:
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// This is the operation that blocks the main thread, so we execute it in a background thread
id item = [rssItems objectAtIndex:floor(indexPath.row/2)];
// UIKit calls need to be made on the main thread, so re-dispatch there
dispatch_async(dispatch_get_main_queue(), ^{
detailViewController.item = item;
[spinner stopAnimating];
});
});
And +1 to @wattson12 - you need to add the spinner to the new view instead. Alternatively you could add the spinner to the current view, and instead put the pushViewController
call into your GCD main-queue block.
+1 到@wattson12 - 您需要将微调器添加到新视图中。或者,您可以将微调器添加到当前视图,并将pushViewController
调用放入 GCD 主队列块中。
Final point - you'll want to remove the spinner from its superview once you stop it animating. Alternatively, you can have a single instance of the spinner, and set hidesWhenStopped
to YES.
最后一点 - 一旦停止动画,您将希望从其超级视图中删除微调器。或者,您可以拥有一个微调器实例,并设置hidesWhenStopped
为 YES。
回答by Javier Calatrava Llavería
This is a spinning wheel over a blurred view in swift:
这是一个在 swift 模糊视图上旋转的轮子:
func blurScence(){
let blurEffect: UIBlurEffect = UIBlurEffect(style: .Dark)
let blurView: UIVisualEffectView = UIVisualEffectView(effect: blurEffect)
blurView.translatesAutoresizingMaskIntoConstraints = false
blurView.frame = self.view.frame
let spinner = UIActivityIndicatorView(activityIndicatorStyle:.White)
spinner.center=blurView.center
blurView.addSubview(spinner)
spinner.startAnimating()
self.view.addSubview(blurView)
}