SVProgressHUD
SVProgressHUD
是一个干净且易于使用的 HUD,用于在 iOS 和 tvOS 上显示正在进行任务的状态。
演示
在 Appetize.io 上尝试 SVProgressHUD
。
安装
从 CocoaPods 安装
CocoaPods 是 Objective-C 的依赖管理工具,它自动化并简化了在项目中使用类似 SVProgressHUD
这样的第三方库的过程。首先,将以下行添加到您的 Podfile
pod 'SVProgressHUD'
如果想要使用 SVProgressHUD
的最新特性,请使用正常的源依赖项。
pod 'SVProgressHUD', :git => 'https://github.com/SVProgressHUD/SVProgressHUD.git'
这将直接从 master
分支拉取。
其次,将 SVProgressHUD
安装到您的项目中
pod install
Carthage
Carthage 是一种去中心化的依赖管理器,可以构建您的依赖项并为您提供二进制框架。要使用 Carthage 将 SVProgressHUD
集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它。
github "SVProgressHUD/SVProgressHUD"
运行 carthage update
构建框架,并将构建的 SVProgressHUD.framework
(位于 Carthage/Build/iOS 文件夹中)拖动到您的 Xcode 项目中(在 Targets
中的 Linked Frameworks and Libraries
)。
手动
- 将
SVProgressHUD/SVProgressHUD
文件夹拖动到项目中。 - 请确保将
SVProgressHUD.bundle
添加到Targets -> Build Phases -> Copy Bundle Resources
。 - 将 QuartzCore 框架添加到项目中。
Swift
尽管 SVProgressHUD
是用 Objective-C 编写的,但它可以无缝地用于 Swift。如果您使用 CocoaPods,请在您的 Podfile 中添加以下行
use_frameworks!
如果您手动添加了 SVProgressHUD
,只需将一个带有 SVProgressHUD
头文件的 bridging header 文件添加到您的项目中。
用法
(参见位于 /Demo
的示例 Xcode 项目)
SVProgressHUD
被创建为单例(即不需要显式分配和实例化;您直接调用 [SVProgressHUD method]
)。
请明智地使用 SVProgressHUD
!仅在使用户绝对需要在用户前进之前执行任务时使用它。不良用例示例:下拉刷新、无限滚动、发送信息。
在您的应用中使用 SVProgressHUD
通常看起来就像这样(使用 Grand Central Dispatch)
[SVProgressHUD show];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// time-consuming task
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
});
});
显示用户界面
您可以使用以下方法之一显示待定任务的状态:
+ (void)show;
+ (void)showWithStatus:(NSString*)string;
如果您想让HUD反映任务进度,请使用以下方法之一:
+ (void)showProgress:(CGFloat)progress;
+ (void)showProgress:(CGFloat)progress status:(NSString*)status;
取消显示HUD
您可以使用以下方式取消显示HUD:
+ (void)dismiss;
+ (void)dismissWithDelay:(NSTimeInterval)delay;
如果您想堆叠 HUD,可以平衡每个显示调用,使用以下方式:
+ (void)popActivity;
一旦 popActivity
调用次数与显示调用次数匹配,HUD 将会被取消显示。
或者在稍后取消显示之前显示确认图标。显示时间取决于 minimumDismissTimeInterval
和给定字符串的长度。
+ (void)showInfoWithStatus:(NSString*)string;
+ (void)showSuccessWithStatus:(NSString*)string;
+ (void)showErrorWithStatus:(NSString*)string;
+ (void)showImage:(UIImage*)image status:(NSString*)string;
自定义
SVProgressHUD
可以通过以下方法进行自定义:
+ (void)setDefaultStyle:(SVProgressHUDStyle)style; // default is SVProgressHUDStyleLight
+ (void)setDefaultMaskType:(SVProgressHUDMaskType)maskType; // default is SVProgressHUDMaskTypeNone
+ (void)setDefaultAnimationType:(SVProgressHUDAnimationType)type; // default is SVProgressHUDAnimationTypeFlat
+ (void)setContainerView:(UIView*)containerView; // default is window level
+ (void)setMinimumSize:(CGSize)minimumSize; // default is CGSizeZero, can be used to avoid resizing
+ (void)setRingThickness:(CGFloat)width; // default is 2 pt
+ (void)setRingRadius:(CGFloat)radius; // default is 18 pt
+ (void)setRingNoTextRadius:(CGFloat)radius; // default is 24 pt
+ (void)setCornerRadius:(CGFloat)cornerRadius; // default is 14 pt
+ (void)setFont:(UIFont*)font; // default is [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline]
+ (void)setForegroundColor:(UIColor*)color; // default is [UIColor blackColor], only used for SVProgressHUDStyleCustom
+ (void)setBackgroundColor:(UIColor*)color; // default is [UIColor whiteColor], only used for SVProgressHUDStyleCustom
+ (void)setBackgroundLayerColor:(UIColor*)color; // default is [UIColor colorWithWhite:0 alpha:0.4], only used for SVProgressHUDMaskTypeCustom
+ (void)setInfoImage:(UIImage*)image; // default is the bundled info image provided by Freepik
+ (void)setSuccessImage:(UIImage*)image; // default is bundled success image from Freepik
+ (void)setErrorImage:(UIImage*)image; // default is bundled error image from Freepik
+ (void)setViewForExtension:(UIView*)view; // default is nil, only used if #define SV_APP_EXTENSIONS is set
+ (void)setMinimumDismissTimeInterval:(NSTimeInterval)interval; // default is 5.0 seconds
+ (void)setMaximumDismissTimeInterval:(NSTimeInterval)interval; // default is infinite
+ (void)setFadeInAnimationDuration:(NSTimeInterval)duration; // default is 0.15 seconds
+ (void)setFadeOutAnimationDuration:(NSTimeInterval)duration; // default is 0.15 seconds
+ (void)setMaxSupportedWindowLevel:(UIWindowLevel)windowLevel; // default is UIWindowLevelNormal
此外,SVProgressHUD
支持大多数上述方法的 UIAppearance
协议。
提示
作为标准配置,SVProgressHUD
提供了两种预配置样式:
SVProgressHUDStyleLight
:白色背景,黑色转动器和文本SVProgressHUDStyleDark
:黑色背景,白色转动器和文本
如果您想使用自定义颜色,请使用 setForegroundColor
和 setBackgroundColor:
。这隐式地将 HUD 的样式设置为 SVProgressHUDStyleCustom
。
通知
SVProgressHUD
通过 NSNotificationCenter
发布四个通知,以响应当前/取消显示事件:
SVProgressHUDWillAppearNotification
在显示动画开始时触发SVProgressHUDDidAppearNotification
在显示动画完成时触发SVProgressHUDWillDisappearNotification
在取消显示动画开始时触发SVProgressHUDDidDisappearNotification
在取消显示动画完成时触发
每个通知都传递一个包含 HUD 状态字符串(如果有的话)的 userInfo
字典,可通过 SVProgressHUDStatusUserInfoKey
获取。
SVProgressHUD
还在用户触摸整个屏幕时发布 SVProgressHUDDidReceiveTouchEventNotification
,用户直接触摸 HUD 时发布 SVProgressHUDDidTouchDownInsideNotification
。对于这些通知,不传递 userInfo
,但对象参数包含相关触摸事件的 UIEvent
。
应用扩展
在使用.App扩展中的SVProgressHUD
时,请使用ивания SV_APP_EXTENSIONS
以避免使用不可用的API。另外,从您的扩展视图控制器中调用setViewForExtension:
方法,参数为self.view
。
对此项目的贡献
如果您有功能请求或错误报告,请随时通过发送pull请求或通过创建新问题来帮助解决。请花一些时间来查看由Nicolas Gallagher编写的指南。
许可证
SVProgressHUD
根据MIT许可证
的条款和条件分发。成功、错误和信息图标由Freepik提供,授权于Flaticon,受创意共享BY 3.0许可。
致谢
SVProgressHUD
由Sam Vermette、Tobias Tiemerding及此项目的贡献者共同呈现。如果您在项目中使用SVProgressHUD
,相应的致谢将会非常感激。