iOS-顶部提示框源码分享,在顶部弹出提示框

属性声明:

UIWindow *_window;
// 窗口的高度
#define XWWindowHeight 20
// 动画的执行时间
#define XWDuration 0.5
// 窗口的停留时间
#define XWDelay 1.5
// 字体大小
#define XWFont [UIFont systemFontOfSize:12]

方法实现:

/** * 显示信息 * * @param msg 文字内容 * @param image 图片对象 */
+ (void)showMessage:(NSString *)msg image:(UIImage *)image
{

    if (_window) return;

    // 创建按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    // 设置按钮文字大小
    btn.titleLabel.font = XWFont;

    // 切掉文字左边的 10
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

    // 设置数据
    [btn setTitle:msg forState:UIControlStateNormal];
    [btn setImage:image forState:UIControlStateNormal];

    // 创建窗口
    _window = [[UIWindow alloc] init];
    // 窗口背景
    _window.backgroundColor = [UIColor blackColor];
    _window.windowLevel = UIWindowLevelAlert;
    _window.frame = CGRectMake(0, -XWWindowHeight, [UIScreen mainScreen].bounds.size.width, XWWindowHeight);
    btn.frame = _window.bounds;
    [_window addSubview:btn];
    _window.hidden = NO;

    // 状态栏 也是一个window
    // UIWindowLevelAlert > UIWindowLevelStatusBar > UIWindowLevelNormal

    // 动画
    [UIView animateWithDuration:XWDuration animations:^{
        CGRect frame = _window.frame;
        frame.origin.y = 0;
        _window.frame = frame;
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:XWDuration delay:XWDelay options:kNilOptions animations:^{
            CGRect frame = _window.frame;
            frame.origin.y = -XWWindowHeight;
            _window.frame = frame;
        } completion:^(BOOL finished) {
            _window = nil;
        }];
    }];
}

你可能感兴趣的:(动画,源码分享,iOS界面)