打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
iOS中可以设定时间自动消失提示框的三种实现方式
  • //==============================================================================  
  •   
  • #pragma mark - 一、显示定制View的消息,定时消失  
  •   
  • //==============================================================================  
  •   
  •   
  • +(void)showMessage:(NSString *)message duration:(NSTimeInterval)time  
  • {  
  •     CGSize screenSize = [[UIScreen mainScreen] bounds].size;  
  •   
  •     UIWindow * window = [UIApplication sharedApplication].keyWindow;  
  •     UIView *showview =  [[UIView alloc]init];  
  •     showview.backgroundColor = [UIColor grayColor];  
  •     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];  
  •     NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];  
  •     paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;  
  •       
  •     NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15.f],  
  •                                    NSParagraphStyleAttributeName:paragraphStyle.copy};  
  •       
  •     CGSize labelSize = [message boundingRectWithSize:CGSizeMake(207, 999)  
  •                                              options:NSStringDrawingUsesLineFragmentOrigin  
  •                                                  attributes:attributes context:nil].size;  
  •    
  •     label.frame = CGRectMake(10, 5, labelSize.width +20, labelSize.height);  
  •     label.text = message;  
  •     label.textColor = [UIColor whiteColor];  
  •     label.textAlignment = 1;  
  •     label.backgroundColor = [UIColor clearColor];  
  •     label.font = [UIFont boldSystemFontOfSize:15];  
  •     [showview addSubview:label];  
  •       
  •     showview.frame = CGRectMake((screenSize.width - labelSize.width - 20)/2,  
  •                                     screenSize.height - 100,  
  •                                        labelSize.width+40,  
  •                                            labelSize.height+10);  
  •     [UIView animateWithDuration:time animations:^{  
  •         showview.alpha = 0;  
  •     } completion:^(BOOL finished) {  
  •         [showview removeFromSuperview];  
  •     }];  
  • }  
  •   
  •   
  • //==============================================================================  
  •   
  • #pragma mark - 二、显示UIAlert窗口消息,定时消失  
  •   
  • //==============================================================================  
  •   
  • //------------------------------------------------------------------------------  
  •   
  • #pragma mark - 1、外部调用接口  
  •   
  • //------------------------------------------------------------------------------  
  •   
  • +(void)showAlert:(NSString *) message duration:(NSTimeInterval)time  
  • {  
  •     UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"提示:"  
  •                                                           message:message delegate:nil  
  •                                                                cancelButtonTitle:nil  
  •                                                                    otherButtonTitles:nil];  
  •       
  •     [NSTimer scheduledTimerWithTimeInterval:time  
  •                                      target:self  
  •                                         selector:@selector(timerFireMethod:)  
  •                                           userInfo:promptAlert  
  •                                              repeats:YES];  
  •     [promptAlert show];  
  • }  
  •   
  • //------------------------------------------------------------------------------  
  •   
  • #pragma mark - 2、外部调用接口的回调方法  
  •   
  • //------------------------------------------------------------------------------  
  •   
  • +(void)timerFireMethod:(NSTimer*)theTimer//弹出框  
  • {  
  •     UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];  
  •     [promptAlert dismissWithClickedButtonIndex:0 animated:NO];  
  •     promptAlert = NULL;  
  • }  
  •   
  •   
  • //==============================================================================  
  •   
  • #pragma mark - 三、显示UIAlert窗口消息,定时消失  
  •   
  • //==============================================================================  
  •   
  • //------------------------------------------------------------------------------  
  •   
  • #pragma mark - 1、外部调用接口  
  •   
  • //------------------------------------------------------------------------------  
  •   
  • +(void)showAlertMessageWithMessage:(NSString*)message duration:(NSTimeInterval)time  
  • {  
  •     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:message delegate:nil  
  •                                           cancelButtonTitle:nil otherButtonTitles:nil];  
  •     [alert show];  
  •     [self performSelector:@selector(dimissAlert:) withObject:alert afterDelay:time];  
  • }  
  •   
  • //------------------------------------------------------------------------------  
  •   
  • #pragma mark - 2、外部调用接口的回调方法  
  •   
  • //------------------------------------------------------------------------------  
  •   
  • +(void) dimissAlert:(UIAlertView *)alert {  
  •     if(alert)     {  
  •         [alert dismissWithClickedButtonIndex:[alert cancelButtonIndex] animated:YES];  
  •     }  
  • }  
  • 本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
    打开APP,阅读全文并永久保存 查看更多类似文章
    猜你喜欢
    类似文章
    【热】打开小程序,算一算2024你的财运
    UIAlertView自动消失
    实例编程iPhone 录音和播放
    ios中提示信息的实现及自动消失
    UILabel自适应高度和自动换行
    UIActionSheet的使用和系统相机的相关调用
    Ios开发中UILocalNotification实现本地通知实现提醒功能
    更多类似文章 >>
    生活服务
    热点新闻
    分享 收藏 导长图 关注 下载文章
    绑定账号成功
    后续可登录账号畅享VIP特权!
    如果VIP功能使用有故障,
    可点击这里联系客服!

    联系客服