Answers:
在UIApplication中:
对于目标C:
开始:
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
结束:
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
为了迅速:
开始
UIApplication.shared.isNetworkActivityIndicatorVisible = true
结束
UIApplication.shared.isNetworkActivityIndicatorVisible = false
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
//作为一个班轮
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
我发现以下宏非常有用!
#define ShowNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
#define HideNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = NO
因此,您可以直接在应用程序中调用ShowNetworkActivityIndicator();
或HideNetworkActivityIndicator();
从应用程序中调用(当然,只要包含标题即可!)。
我写了一个单例,通过保留正在发生的事情来解决多个连接的问题(避免在连接返回但另一个仍处于活动状态时删除状态):
头文件:
#import <Foundation/Foundation.h>
@interface RMActivityIndicator : NSObject
-(void)increaseActivity;
-(void)decreaseActivity;
-(void)noActivity;
+(RMActivityIndicator *)sharedManager;
@end
和实现:
#import "RMActivityIndicator.h"
@interface RMActivityIndicator ()
@property(nonatomic,assign) unsigned int activityCounter;
@end
@implementation RMActivityIndicator
- (id)init
{
self = [super init];
if (self) {
self.activityCounter = 0;
}
return self;
}
-(void)increaseActivity{
@synchronized(self) {
self.activityCounter++;
}
[self updateActivity];
}
-(void)decreaseActivity{
@synchronized(self) {
if (self.activityCounter>0) self.activityCounter--;
}
[self updateActivity];
}
-(void)noActivity{
self.activityCounter = 0;
[self updateActivity];
}
-(void)updateActivity{
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = (self.activityCounter>0);
}
#pragma mark -
#pragma mark Singleton instance
+(RMActivityIndicator *)sharedManager {
static dispatch_once_t pred;
static RMActivityIndicator *shared = nil;
dispatch_once(&pred, ^{
shared = [[RMActivityIndicator alloc] init];
});
return shared;
}
@end
例:
[[RMActivityIndicator sharedManager]increaseActivity];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:self.networkReceiveProcessQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
[[RMActivityIndicator sharedManager]decreaseActivity];
}
网络通话完成后,您还需要注意隐藏活动指示器。
如果您使用AFNetworking
,则不需要做很多事情。
在AppDelegate
类中进行以下更改:
进口 AFNetworking/AFNetworkActivityIndicatorManager.h
放进去 didFinishLaunchingWithOptions:
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]
正如许多人所说,没有针对iPhone X的网络活动指标,也可能没有针对其他带有缺口的新iPhone的网络活动指标。
我遇到了由Ortwin Gentz编写的FutureTap令人难以置信的库:https : //github.com/futuretap/FTLinearActivityIndicator
它将指示器放回到最初发布iPhone X的位置,许多人会记得Knight Rider类型的指示器。
该库可用于Swift 4.2,因此您需要按以下说明更改Swift语言设置: 类型'NSAttributedStringKey'(aka'NSString')没有成员'font'