蘋果自帶的風火輪如果不是那麼「樸素」的話,我們大概也不需要去定製它。可惜 UIActivityIndicatorView 只有 initWithActivityIndicatorStyle: 這麼一個初始化入口,帶來兩個明顯的限制:
尺寸無法自由調整 —— 有時候我們需要一個比 UIActivityIndicatorViewStyleWhiteLarge 更大的指示器。
信息承載能力不足 —— 我們往往需要在風火輪下方顯示一段提示文案,告訴用戶正在發生什麼。
於是就有了自己造一個的需求。整體思路是:用一個 UIView 作為容器,內部放一個 UIActivityIndicatorView 和一個 UILabel,再用 Quartz 繪圖畫出半透明圓角背景。
新建 MyProgressView 類繼承自 UIView,作為整個 HUD 的載體。
重寫 initWithFrame:superView:,根據傳入的坐標和尺寸佈局,固定 indicator 與 label 的位置大小,並掛到外部容器 viewHud 上,最後把自己加到父視圖。
重寫 drawRect:,判斷可見性後繪製圓角矩形:用 CGContext 相關 API 構造路徑並填充顏色。由於該方法在視圖顯示之後執行,我們可以在其中調用 CGRectOffset 完成居中定位。
提供 show:、hide、setMessage: 三個方法對外暴露控制能力。
alignToCenter 負責把整個 HUD 移到父視圖正中間。
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface MyProgressView : UIView {
UIActivityIndicatorView *indicator;
UILabel *label;
BOOL visible, blocked;
UIView *maskView;
CGRect rectHud, rectSuper, rectOrigin; // 外殼區域、父視圖區域
UIView *viewHud; // 外殼
}
@property (assign) BOOL visible;
- (id)initWithFrame:(CGRect)frame superView:(UIView *)superView;
- (void)show:(BOOL)block; // block: 是否阻塞父視圖交互
- (void)hide;
- (void)setMessage:(NSString *)newMsg;
- (void)alignToCenter;
@end
#import "MyProgressView.h"
@implementation MyProgressView
@synthesize visible;
- (id)initWithFrame:(CGRect)frame superView:(UIView *)superView {
rectOrigin = frame;
rectSuper = [superView bounds];
// 保持正方形比例
rectHud = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.width);
self = [super initWithFrame:rectHud];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
viewHud = [[UIView alloc] initWithFrame:rectHud];
[self addSubview:viewHud];
indicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
double gridUnit = round(rectHud.size.width / 12);
float ind_width = 6 * gridUnit;
indicator.frame = CGRectMake(3 * gridUnit, 2 * gridUnit, ind_width, ind_width);
[viewHud addSubview:indicator];
CGRect rectLabel = CGRectMake(1 * gridUnit, 9 * gridUnit, 10 * gridUnit, 2 * gridUnit);
label = [[UILabel alloc] initWithFrame:rectLabel];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@"Arial" size:14];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = @"請等待...";
label.adjustsFontSizeToFitWidth = YES;
[viewHud addSubview:label];
visible = NO;
[self setHidden:YES];
[superView addSubview:self];
}
return self;
}
#pragma mark - Drawing
- (void)drawRect:(CGRect)rect {
if (visible) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect boxRect = rectHud;
// 繪製圓角矩形
float radius = 10.0f;
CGContextBeginPath(context);
// 填充色:灰度 0.0,透明度 0.25
CGContextSetGrayFillColor(context, 0.0f, 0.25);
// 移動到左上角圓弧起點
CGContextMoveToPoint(context,
CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect));
// 右上角
CGContextAddArc(context,
CGRectGetMaxX(boxRect) - radius, CGRectGetMinY(boxRect) + radius, radius,
3 * (float)M_PI / 2, 0, 0);
// 右下角
CGContextAddArc(context,
CGRectGetMaxX(boxRect) - radius, CGRectGetMaxY(boxRect) - radius, radius,
0, (float)M_PI / 2, 0);
// 左下角
CGContextAddArc(context,
CGRectGetMinX(boxRect) + radius, CGRectGetMaxY(boxRect) - radius, radius,
(float)M_PI / 2, (float)M_PI, 0);
// 左上角
CGContextAddArc(context,
CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect) + radius, radius,
(float)M_PI, 3 * (float)M_PI / 2, 0);
// 填充路徑,該函數會自動關閉路徑
CGContextFillPath(context);
// 繪製結束後把畫面放到屏幕正中間
[self alignToCenter];
}
}
#pragma mark - Action
- (void)show:(BOOL)block {
if (block && blocked == NO) {
CGPoint offset = self.frame.origin;
// 把視圖撐滿到父視圖大小,以便攔截所有觸摸事件
self.frame = rectSuper;
viewHud.frame = CGRectOffset(viewHud.frame, offset.x, offset.y);
if (maskView == nil) {
maskView = [[UIView alloc] initWithFrame:rectSuper];
} else {
maskView.frame = rectSuper;
}
maskView.backgroundColor = [UIColor clearColor];
[self addSubview:maskView];
[self bringSubviewToFront:maskView];
blocked = YES;
}
[indicator startAnimating];
[self setHidden:NO];
[self setNeedsLayout];
visible = YES;
}
- (void)hide {
visible = NO;
[indicator stopAnimating];
[self setHidden:YES];
}
- (void)setMessage:(NSString *)newMsg {
label.text = newMsg;
}
- (void)alignToCenter {
CGPoint centerSuper = {rectSuper.size.width / 2, rectSuper.size.height / 2};
CGPoint centerSelf = {self.frame.origin.x + self.frame.size.width / 2,
self.frame.origin.y + self.frame.size.height / 2};
CGPoint offset = {centerSuper.x - centerSelf.x, centerSuper.y - centerSelf.y};
CGRect newRect = CGRectOffset(self.frame, offset.x, offset.y);
[self setFrame:newRect];
rectHud = newRect;
}
@end
幾處設計值得說明:以 gridUnit = width / 12 為基準單位來計算各子視圖的 frame,這樣調整整體尺寸時內部佈局會自動等比縮放;show: 中的 block 參數用來控制是否遮罩父視圖並攔截交互,模態提示時傳 YES,非模態時傳 NO。
- (void)viewDidLoad {
[super viewDidLoad];
// 系統自帶的太小了,換成自定義的
// UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]
// initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
// [indicator setCenter:CGPointMake(100, 100)];
// [self.view addSubview:indicator];
// [indicator startAnimating];
MyProgressView *indicator = [[MyProgressView alloc]
initWithFrame:CGRectMake(0, 0, 120, 120) superView:self.view];
[indicator setMessage:@"正在接收心電數據請稍候..."];
if (indicator.visible == NO) {
[indicator show:NO];
} else {
[indicator hide];
}
}
頁面加載時就讓 HUD 出現,用 [indicator hide] 即可隱藏。
這個實現把「顯示狀態」與「佈局計算」耦合在了 drawRect: 中 —— 每次重繪都會觸發一次居中計算,屬於副作用,並不是好習慣。更清晰的做法是把居中邏輯移到 show: 裡顯式調用,讓 drawRect 只負責繪製。另外,在真實項目中,這類 HUD 通常直接用 MBProgressHUD 等成熟庫,能省下不少邊角問題的處理成本。