码迷,mamicode.com
首页 > 移动开发 > 详细

在iOS中实现类似安卓自动消失提示框

时间:2016-09-18 10:08:10      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

类方法:

+ (void)showMessage:(NSString *)message {
    // 获取window
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *showView = [[UIView alloc] init];
    showView.backgroundColor = [UIColor blackColor];
    showView.frame = CGRectMake(1, 1, 1, 1);
    showView.alpha = 1.0f;
    showView.layer.cornerRadius = 5.0f;
    showView.layer.masksToBounds = YES;
    [window addSubview:showView];
    
    UILabel *label = [[UILabel alloc] init];
    UIFont *font = [UIFont systemFontOfSize:15];
    CGRect labelRect = [message boundingRectWithSize:CGSizeMake(290, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil];
    label.frame = CGRectMake(10, 5, CGRectGetWidth(labelRect), CGRectGetHeight(labelRect));
    label.text = message;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    [showView addSubview:label];
    showView.frame = CGRectMake((SCREEN_WIDTH - CGRectGetWidth(labelRect) - 20)/2, SCREEN_HEIGHT - 100, CGRectGetWidth(labelRect)+20, CGRectGetHeight(labelRect)+10);
    [UIView animateWithDuration:1.5 animations:^{
        showView.alpha = 0;
    } completion:^(BOOL finished) {
        [showView removeFromSuperview];
    }];
    
}

 

在iOS中实现类似安卓自动消失提示框

标签:

原文地址:http://www.cnblogs.com/mafeng/p/5880415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!