个样例,我们选取纯文本、加载(菊花)、条状进度条和自定义视图进行分析,其他的样例与它们类似。
1.纯文本(Text)
我们先从最简单的纯文本开始。启动HUDDemo项目,点开MBHudDemoViewController.m
文件,找到函数- (void)textExample{…}
,这个函数就是显示纯文本的处理函数:
- (void)textExample {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
// Set the text mode to show only text.
hud.mode = MBProgressHUDModeText;
hud.label.text = NSLocalizedString(@"Message here!", @"HUD message title");
// Move to bottm center.
hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
[hud hideAnimated:YES afterDelay:3.f];
}
① 进入到函数showHUDAddedTo:animated:
中查看MBProgressHUD实例的创建过程:
initWithView:
->initWithFrame:
->commonInit
使用self.navigationController.view
的bounds初始化HUD,然后在commonInit
里指定动画类型(Fade)、HUD模式(菊花)、间距(20)、内容颜色(黑色半透明)。除此之外,还设置HUD为完全透明,背景色为clear,配置HUD的尺寸自动调整:
//保证上下间距比例不变、左右间距比例不变,即防止横竖屏切换时HUD位置错误
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//让HUD的各个子视图自己控制自己的透明度,使其不受HUD透明度的影响
self.layer.allowsGroupOpacity = NO;
[self setupViews]
在这个函数中真正执行子视图的创建工作。
背景视图(backgroundView)
为类MBBackgroundView
的实例。MBBackgroundView
实例默认会创建成白色半透明模糊效果,并覆盖全屏,但在本例中,创建完成之后会更改其style
为MBProgressHUDBackgroundStyleSolidColor
,并将背景色设置为透明(clear)。
内容框(bezelView)
同为类MBBackgroundView
实例,是实际展示内容的View(即中间的黑框),包含文本、indicator、进度条等。bezelView
会默认创建成白色半透明模糊效果,但frame为0。创建后会设置其边角半径为5。
知识点:作者为bezelView添加了MotionEffect,也就是说在bezelView显示的时候,它会根据手机的倾斜方向调整自己的位置!
- (void)updateBezelMotionEffects {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 || TARGET_OS_TV
MBBackgroundView *bezelView = self.bezelView;
if (![bezelView respondsToSelector:@selector(addMotionEffect:)]) return;
if (self.defaultMotionEffectsEnabled) {
CGFloat effectOffset = 10.f;
UIInterpolatingMotionEffect *effectX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
effectX.maximumRelativeva lue = @(effectOffset);
effectX.minimumRelativeva lue = @(-effectOffset);
UIInterpolatingMotionEffect *effectY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
effectY.maximumRelativeva lue = @(effectOffset);
effectY.minimumRelativeva lue = @(-effectOffset);
UIMotionEffectGroup *group = [[UIMotionEffectGroup alloc] init];
group.motionEffects = @[effectX, effectY];
[bezelView addMotionEffect:group];
} else {
NSArray *effects = [bezelView motionEffects];
for (UIMotionEffect *effect in effects) {
[bezelView removeMotionEffect:effect];
}
}
#endif
}
label和detailsLabel
设置显示文字的label,其中detailsLabel允许多行。
button
为MBProgressHUDRoundedButton
的实例,作为HUD上的功能按钮,比如进度条下方可以显示一个"取消"按钮。
topSpacer和bottomSpacer
均为UIView
的实例,用于调节上下间距的辅助View。
设置label、detailsLabel及button的抗压系数,并添加到父视图上。
for (UIView *view in @[label, detailsLabel, button]) {
view.translatesAutoresizingMaskIntoConstraints = NO;//自己手动管理约束
[view setContentCompressionResistancePriority:998.f forAxis:UILayoutConstraintAxisHorizontal];//设置水平抗压缩系数,值越大,越不容易被压缩
[view setContentCompressionResistancePriority:998.f forAxis:UILayoutConstraintAxisVertical];//设置垂直抗压缩系数,值越大,越不容