BTBalloon是一个自定义的UIView子类,能够在气球样式的弹窗中显示文本和可选的图片以及/或按钮。您可以使用此视图作为工具提示以突出显示应用程序UI的各个部分,或创建交互式教程(见示例项目)。将其视为ArialView和UIPopover的中间。
通过调用-showWithTitle...
方法之一,可以轻松显示BTBalloon。例如,要显示一个带有图片、标题和按钮的气球并使其锚定到现有视图,您将输入以下命令
[[BTBalloon sharedInstance] showWithTitle:@"Hello World" image:[UIImage imageNamed:@"Icon"] anchorToView:self.myExistingView];
无需直接实例化BTBalloon,而是只需使用sharedInstance
。您不需要将BTBalloon视图直接添加到视图层次结构中,因为在不调用任何-showWith
方法的情况下,这会由您自动处理。我们将自动在所有其他视图之上添加视图。
请注意,当使用sharedInstance
时,屏幕上一次只能出现一个BTBalloon视图。如果想要显示多个气球视图,可以像下面那样创建多个类的实例。
要同时显示多个气球视图或自定义气球视图的大小,只需调用专门的类初始化器以创建自己的BTBalloon实例
BTBalloon *balloon = [BTBalloon new];
[balloon showWithTitle:@"Hello World" image:[UIImage imageNamed:@"Icon"] anchorToView:self.myExistingView];
或者
BTBalloon *balloon = [BTBalloon alloc] initWithFrame:CGRectMake(0, 0, 300, 200);
[balloon showWithTitle:@"Hello World" image:[UIImage imageNamed:@"Icon"] anchorToView:self.myExistingView];
BTBalloon遵循UIAppearance协议。有关UIAppearance的更多信息,请参阅:[https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAppearance_Protocol/index.html](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAppearance_Protocol/index.html)
要自定义外观,只需设置BTBalloon类appearance
代理的相关样式相关属性。这些语句的最佳添加位置是在AppDelegate的application:didFinishLaunchingWithOptions:
方法中。
[BTBalloon appearance].buttonFont = [UIFont fontWithName:@"HelveticaNeue-Medium" size:18.0f];
[BTBalloon appearance].buttonTextColor = [UIColor colorWithWhite:0.279 alpha:1.000];
[BTBalloon appearance].buttonBackgroundColor = [UIColor colorWithWhite:0.922 alpha:1.000];
[BTBalloon appearance].balloonBackgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:0.950];
[BTBalloon appearance].textFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:19.0f];
[BTBalloon appearance].textColor = [UIColor whiteColor];
在屏幕上显示带有标题和输入的图片的气球。
- (void)showWithTitle:(NSString *)title image:(UIImage *)image;
显示带标题和图像的气球,相对于锚定视图。
- (void)showWithTitle:(NSString *)title image:(UIImage *)image anchorToView:(UIView *)view;
显示带标题、图像和按钮的气球,相对于锚定视图。提供按钮按下时执行的回调块。
- (void)showWithTitle:(NSString *)title image:(UIImage *)image anchorToView:(UIView *)view buttonTitle:(NSString *)buttonTitle buttonCallback:(void (^)(void))callbackBlock;
延迟后显示带标题、图像和按钮的气球,相对于锚定视图。提供按钮按下时执行的回调块。
- (void)showWithTitle:(NSString *)title image:(UIImage *)image anchorToView:(UIView *)view buttonTitle:(NSString *)buttonTitle buttonCallback:(void (^)(void))callbackBlock afterDelay:(NSTimeInterval)delay;
更新当前气球的标题、图像、按钮和回调。
- (void)updateTitle:(NSString *)title image:(UIImage *)image button:(NSString *)buttonTitle buttonCallback:(void (^)(void))callbackBlock;
将现有气球移动到相对于锚定视图的新位置。
- (void)anchorToView:(UIView *)view;
使用动画隐藏气球。气球并没有从父视图中被移除,而是通过调整透明度和隐藏属性来隐藏。永远不要调用 removeFromSuperview
,因为这会导致未来尝试显示气球失败。
- (void)hide;
与 -hide
相同,但可选项包含动画切换。
- (void)hideWithAnimation:(BOOL)animated;
使用动画显示气球。视图将自动添加到您的应用视图层次结构中,因此永远不会在父视图中调用 addSubview:
,传入 BTBalloon 实例,否则您会得到不期望的行为。
- (void)show;
与 -show
相同,但可选项包含动画切换。
- (void)showWithAnimation:(BOOL)animated;
包含一个示例项目,展示了如何使用 BTBalloon 通过气球创建交互式引导,帮助用户通过在应用中执行一系列动作的过程。
推荐通过 CocoaPods 包管理器安装 BTBalloon,因为它提供了灵活的依赖管理以及非常简单的安装。或者,您可以只是克隆 Git 仓库并将源文件添加到项目中。
BTBalloon 根据 Apache 许可协议版本 2.0 许可。请参阅 LICENSE 文件以获取详细信息。
BTBalloon 由 https://github.com/brightec 提供。