網路查詢的耗時或長或短,為了提升用戶體驗,常見做法之一就是在等待時加入一個動態加載效果。當我們請求網路、載入頁面時呈現一個動畫,效果如下:

原始碼可參考開源專案 Coding.net。其原理是兩張圖片組合:外圈負責旋轉動畫,內圈圖示則變化透明度。以下為主要實作。
1. 封裝於 EaseLoadingView 中
@interface EaseLoadingView : UIView
@property (strong, nonatomic) UIImageView *loopView, *monkeyView;
@property (assign, nonatomic, readonly) BOOL isLoading;
- (void)startAnimating;
- (void)stopAnimating;
@end
@implementation EaseLoadingView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
_loopView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_loop"]];
_monkeyView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_monkey"]];
[self addSubview:_loopView];
[self addSubview:_monkeyView];
_loopAngle = 0.0;
_monkeyAlpha = 1.0;
_angleStep = 360 / 3;
_alphaStep = 1.0 / 3.0;
}
return self;
}
- (void)startAnimating {
self.hidden = NO;
if (_isLoading) { return; }
_isLoading = YES;
[self loadingAnimation];
}
- (void)stopAnimating {
self.hidden = YES;
_isLoading = NO;
}
- (void)loadingAnimation {
static CGFloat duration = 0.25f;
_loopAngle += _angleStep;
if (_monkeyAlpha >= 1.0 || _monkeyAlpha <= 0.0) {
_alphaStep = -_alphaStep;
}
_monkeyAlpha += _alphaStep;
[UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
_loopView.transform = CGAffineTransformMakeRotation(_loopAngle * (M_PI / 180.0f));
_monkeyView.alpha = _monkeyAlpha;
} completion:^(BOOL finished) {
if (_isLoading && [self superview] != nil) {
[self loadingAnimation];
} else {
[self removeFromSuperview];
}
}];
}
@end
其中 loadingAnimation 負責旋轉與透明度的動畫處理,停止加載後會自動從當前視圖移除。
2. 在 UIView 擴展類中封裝(利用關聯物件)
@interface UIView (Common)
- (void)beginLoading;
- (void)endLoading;
@end
@implementation UIView (Common)
- (void)setLoadingView:(EaseLoadingView *)loadingView {
objc_setAssociatedObject(self, &LoadingViewKey, loadingView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (EaseLoadingView *)loadingView {
return objc_getAssociatedObject(self, &LoadingViewKey);
}
- (void)beginLoading {
if (!self.loadingView) {
self.loadingView = [[EaseLoadingView alloc] initWithFrame:self.bounds];
}
[self addSubview:self.loadingView];
[self.loadingView startAnimating];
}
- (void)endLoading {
if (self.loadingView) {
[self.loadingView stopAnimating];
}
}
@end
補充:Cocoa 的 KVO 有兩種通知觀察者的方式——自動通知與手動通知。自動通知由 Cocoa 在屬性值變化時自動觸發;手動通知則需在值變化時自行呼叫 willChangeValueForKey: 與 didChangeValueForKey:。
3. 頁面中呼叫
- (void)sendRequest {
[self.view beginLoading];
__weak typeof(self) weakSelf = self;
[[Coding_NetAPIManager sharedManager] request_CodeFile:_myCodeFile withPro:_myProject andBlock:^(id data, NSError *error) {
[weakSelf.view endLoading];
if (data) {
weakSelf.myCodeFile = data;
[weakSelf refreshCodeViewData];
}
}];
}
只要在請求前後分別呼叫 beginLoading 與 endLoading,即可輕鬆呈現等待動畫。
補充:序列幀動畫
另一種做法是準備多張連續圖片,透過迴圈載入組成動畫:
NSMutableArray *idleImages = [NSMutableArray array];
for (NSUInteger i = 1; i <= 60; ++i) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd", i]];
[idleImages addObject:image];
[idleImages addObject:image];
}
以上兩種方式——雙圖組合動畫與序列幀動畫——都能有效提升等待時的使用者體驗,可依實際需求靈活選用。