代码改变世界

ios提示框,自动消失

2015-11-07 14:28  xiangjune  阅读(894)  评论(0编辑  收藏  举报

 

-(void)click

{

    NSString *showMsg = @"点击我,开始提示";

    int width = showMsg.length * 20;

    

    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    

    UIView *tipView = [[UIView alloc]init];

    tipView.frame = CGRectMake((window.frame.size.width/2)-width/2, (window.frame.size.height/2)-30, width, 50);

    tipView.backgroundColor = [UIColor blackColor];

    

    UILabel *labelView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width, 50)];

    labelView.text = showMsg;

    labelView.textColor = [UIColor whiteColor];

    labelView.font = [UIFont systemFontOfSize:15];

    labelView.textAlignment = kCTCenterTextAlignment;

    [tipView addSubview:labelView];

    

    [window addSubview:tipView];

    

    [UIView animateWithDuration:2 animations:^{tipView.alpha = 0.0;} completion:^(BOOL finished){[tipView removeFromSuperview];}];

    

}