为 Android Toast 消息设计,易于定制的 iOS 复现。
要使用默认样式的 Toast,只需写
[GKRToast show:@"Your text"];
要使用其他样式,请使用两个类之一 AIMToast
或 AIMFancyToast
或创建自定义子类
AIMToast - 我们在 All in Mobile 中使用的类,背景和文字颜色可以轻松定制
如果您想制作具有自定义布局和动画的 Toast,您应该创建 GKRToast
的子类,重写 + (GKRToastConfigure *)configure
方法并返回新的 Toast 配置。例如,如果您想要一个从左侧出现的 Toast,您应该写如下内容
+ (GKRToastConfigure *)configure {
GKRToastConfigure *configure = [GKRToastConfigure new];
CGFloat offset = CGRectGetWidth([UIScreen mainScreen].bounds) * 0.5f + 100;
configure.animateShowLabel = ^(UIView *textContainer, UILabel *textLabel, MASConstraint *centerX, MASConstraint *centerY) {
centerX.offset = -offset;
[textContainer layoutIfNeeded];
centerX.offset = 0;
[UIView animateWithDuration:1.0f animations:^{
[textContainer layoutIfNeeded];
}];
};
return configure;
};
GKRToastConfigure
对象有以下属性
属性 | 类型 | 描述 | 默认值 |
---|---|---|---|
backgroundView | UIView * | 全屏显示的背景 | 带 30% 不透明的背景颜色 |
textContainer | UIView * | 文本消息容器 | 带 80% 不透明的背景颜色 |
textLabel | UILabel * | 用于显示消息文本的标签 | 白色文字颜色,行数设为 0 |
textEdgeInsets | UIEdgeInsets | textLabel 的外边距 |
左、右、上和下均设为 10 点 |
hideTimeOut | NSTimeInterval | 显示时间(秒);如果设置为 0,则不会自动隐藏 | 5s |
animateShowLabel | void(^)(UIView *textContainer, UILabel *textLabel, MASConstraint *centerX, MASConstraint *centerY) | 在 textContainer 添加到当前窗口后进行块调用;centerX 和 centerY 是使 Toast 在屏幕上居中的约束 |
在 0.5 秒内从 0 到 1 的 alpha 过渡 |
animateShowBackground | void(^)(UIView *backgroundView) | 在 backgroundView 添加到当前窗口后进行块调用 |
在 0.5 秒内从 0 到 1 的 alpha 过渡 |
animateHideLabel | void(^)(UIView *textContainer, UILabel *textLabel, MASConstraint *centerX, MASConstraint *centerY) | block 调用当 textContainer 应该从当前窗口中移除时;动画结束后,请务必调用 [textLabel removeFromSuperview] 和 [textContainer removeFromSuperview] |
在 0.5 秒内从 1 到 0 的 alpha 过渡 |
animateHideBackground | void(^)(UIView *backgroundView) | 当需要从当前窗口删除 backgroundView 时进行block调用;动画结束后请记住调用 [backgroundView removeFromSuperview] |
在 0.5 秒内从 1 到 0 的 alpha 过渡 |