蘋果的「無敵風火輪」(UIActivityIndicatorView)如果不是那麼受限,我們就不需要客製化它了。可惜它只有一個初始化方法 initWithActivityIndicatorStyle,有兩個問題:
一、不能任意改變大小——有時候我們需要一個比 UIActivityIndicatorViewStyleWhiteLarge 還要大的指示器;
二、它不夠友善——我們需要在風火輪下方顯示一些提示資訊。
於是我們不惜代價,用一個 UIActivityIndicatorView 加上 Quartz 繪圖,自製了一個屬於自己的無敵風火輪。
實作的主要思路如下:
1. 先建一個繼承自 UIView 的 MyProgressView 類,在上面加入 Indicator 與文字並繪製背景,再把這個 View 放到當前介面中。
2. 重寫 initWithFrame,根據傳入的座標與大小繪製,固定 Indicator 與 Label 的位置與大小,並加入外部框架 viewHud。
3. 重寫 drawRect,判斷自訂 View 是否可見,可見就繪製圓角矩形(依 CGRect 繪製路徑並用 CGContextFillPath 填色),這個方法會在自訂 View 顯示後執行,因此可在其中做 View 移位,用 CGRectOffset 平移或呼叫 alignToCenter 置中。
4. show、hide、setMessage 主要用來對自訂 View 做進一步操作。
5. alignToCenter 主要把當前自訂 View 置中。
請看源碼。MyProgressView.h:
#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
MyProgressView.m:
#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 = UITextAlignmentCenter;
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, 33 * (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, 33 * (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;
}
#pragma mark 隱藏
- (void)hide {
visible = NO;
[indicator stopAnimating];
[self setHidden:YES];
}
#pragma mark 改變文字
- (void)setMessage:(NSString *)newMsg {
label.text = newMsg;
}
#pragma mark 置中
- (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
那麼如何在頁面中引用上面的程式碼呢?ViewController.m 中的引用程式碼如下:
- (void)viewDidLoad {
[super viewDidLoad];
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];
}
}
若想在頁面載入時就讓 Indicator 出現,呼叫 [indicator show:NO] 即可;要隱藏則呼叫 [indicator hide]。