查詢時間或長或短,為了提升用戶體驗,目前常用的手段之一就是在查詢等待時加入一個動態等待效果。當我們在請求網路、載入頁面時有個動作效果,如下圖:

網上有開源專案(如 Coding.net)可供參考,其原理是兩張圖片組合:外圈做旋轉動畫,內圈圖示則做透明度變化。主要程式碼如下:
@interface EaseLoadingView : UIView
@property (strong, nonatomic) UIImageView *loopView, *monkeyView;
@property (assign, nonatomic, readonly) BOOL isLoading;
- (void)startAnimating;
- (void)stopAnimating;
@end
@interface EaseLoadingView ()
@property (nonatomic, assign) CGFloat loopAngle, monkeyAlpha, angleStep, alphaStep;
@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"]];
[_loopView setCenter:self.center];
[_monkeyView setCenter:self.center];
[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:^{
CGAffineTransform loopAngleTransform = CGAffineTransformMakeRotation(_loopAngle * (M_PI / 180.0f));
_loopView.transform = loopAngleTransform;
_monkeyView.alpha = _monkeyAlpha;
} completion:^(BOOL finished) {
if (_isLoading && [self superview] != nil) {
[self loadingAnimation];
} else {
[self removeFromSuperview];
_loopAngle = 0.0;
_monkeyAlpha = 1.0;
_alphaStep = ABS(_alphaStep);
}
}];
}
@end
注意 loadingAnimation 裡面有動作與透明度的處理,停止載入後會把它從目前視圖移除。
#pragma mark LoadingView
@property (strong, nonatomic) EaseLoadingView *loadingView;
- (void)beginLoading;
- (void)endLoading;
- (void)setLoadingView:(EaseLoadingView *)loadingView {
[self willChangeValueForKey:@"LoadingViewKey"];
objc_setAssociatedObject(self, &LoadingViewKey, loadingView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self didChangeValueForKey:@"LoadingViewKey"];
}
- (EaseLoadingView *)loadingView {
return objc_getAssociatedObject(self, &LoadingViewKey);
}
- (void)beginLoading {
for (UIView *aView in [self.blankPageContainer subviews]) {
if ([aView isKindOfClass:[EaseBlankPageView class]] && !aView.hidden) {
return;
}
}
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];
}
}
注意:Cocoa 的 KVO 模型中有兩種通知觀察者的方式——自動通知與手動通知。自動通知由 Cocoa 在屬性值變化時自動通知觀察者;手動通知則需在值變化時呼叫 willChangeValueForKey: 與 didChangeValueForKey: 方法來通知。
- (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];
}
}];
}
其中 [self.view beginLoading] 與 [weakSelf.view 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];
}