代码之家  ›  专栏  ›  技术社区  ›  David

如何在iPhone/iPad/iOS上显示临时弹出消息

  •  30
  • David  · 技术社区  · 15 年前

    我想在iphone/ipad上显示一条临时消息,显示一个动作的确认信息,或者一些后台活动的快速状态。

    是否有一个标准控件来执行此操作?我见过应用程序这样做。一个圆形矩形,深色,部分透明,内部有文本。它不要求用户输入,但会在短时间内自行消失。 Android有一个类似的标准结构。也类似于咆哮声显示的窗户。

    感谢您的建议。

    10 回复  |  直到 8 年前
        1
  •  15
  •   Lukasz Czerwinski    11 年前

    cocoacontrols.com上有一个用户库,模拟Android风格的Toast弹出窗口。可能是你想要的。

    http://www.cocoacontrols.com/platforms/ios/controls/altoastview

    还有一个遵循同样的想法。

    http://www.cocoacontrols.com/platforms/ios/controls/itoast

        2
  •  15
  •   Goles    14 年前

    创建从继承的类 UIAlertView . 在构造函数中,只需调用 [super init] 然后添加任何视图作为子视图。甚至可以在Interface Builder中设计此视图。像这样:

    - (id)initWithMessage:(NSString *)message dismissAfter:(NSTimeInterval)interval
    {
      if ((self = [super init]))
      {
         CustomView * customView = [[[CustomView alloc] init] autorelease]; // or load from NIB
         [self addSubview:customView];
         [self performSelector:@selector(dismissAfterDelay) withObject:nil afterDelay:interval];
      }
      return self;
    }
    
    - (void)dismissAfterDelay
    {
      [self dismissWithClickedButtonIndex:0 animated:YES]; 
    }
    

    要显示自定义警报视图,只需初始化它,然后调用 show 和普通的一样 uiAlctVIEW .

    CustomAlertView * cav = [[CustomAlertView alloc] initWithMessage:@"Doing Something];
    [cav show];
    [cav release];
    

    作为一个很好的副作用,当你呈现这个视图时,背景会变暗,你会得到任何警报视图的很好的摇摆动画。

        3
  •  9
  •   Kennzo    15 年前

    使用uiAlertView。下面是一个简单示例的快速链接:

    UIAlertView

    编辑:对不起,没看到自己消失。我想你需要用户对收到的消息进行确认。uiAlertView几乎可以实现这一点。不确定你是否会逐渐淡出,除非你在一个视图中有一个计时器或基于事件的延迟,它会显示一个视图,然后最终移除它。

    第二次编辑:找到了实现它的方法。您可以在指定的某个设置时间后手动调用disclose函数。(为这篇乱七八糟的文章感到抱歉)看起来应该是这样的:

    //Create UIAlertView alert
    alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Some message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
    
    //After some time
    [alert dismissWithClickedButtonIndex:0 animated:TRUE];
    
        4
  •  8
  •   async8192    12 年前

    使用具有nil标题和nil按钮的uiAlertView,然后在需要时将其取消。我是这样做的:

    在.h文件中为警报视图创建一个实例变量:

    @interface StatusMessageController : UIViewController {
        UIAlertView *statusAlert;
    }
    

    在.m文件中,创建一个方法以显示警报视图并启动计时器,以及另一个方法以在计时器过期时处理以解除警报:

    - (void)showStatus:(NSString *)message timeout:(double)timeout {
        statusAlert = [[UIAlertView alloc] initWithTitle:nil
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:nil
                                              otherButtonTitles:nil];
        [statusAlert show];
        [NSTimer scheduledTimerWithTimeInterval:timeout
                                         target:self
                                       selector:@selector(timerExpired:)
                                       userInfo:nil
                                        repeats:NO];
    }
    
    - (void)timerExpired:(NSTimer *)timer {
        [statusAlert dismissWithClickedButtonIndex:0 animated:YES];
    }
    

    每当要显示状态消息时,调用它:

    [self showStatus:@"Computing" timeout:4.5];
    

    在任何时候,您也可以通过以下方式解除警报:

    [statusAlert dismissWithClickedButtonIndex:0 animated:YES];
    

    您还可以使用新状态即时更改消息:

    statusAlert.message = @"Looking up user";
    
        5
  •  8
  •   htafoya    12 年前

    我创建了一个android类型的toast,非常简单,因为我现在不需要在里面有更多的功能。

    当显示它时,它被添加到父视图的底部,因此如果该视图是VC的视图,那么它将位于设备的底部中心。

    框架自动调整为文本长度。

    你用它来做: [self.view addSubview: [[ToastAlert alloc] initWithText: @"Sent"]]; ,它将自动删除,因此不需要引用。

    我没有实现这一点,但是您可以创建一个静态方法来缩短和澄清指令,比如: [ToastAlert showText: @"Sent" inView: self.view]; .

    班级:

    TASTAdvest.h

    @interface ToastAlert : UILabel {
    
    }
    
    - (id)initWithText: (NSString*) msg;
    
    @end
    

    托斯特警报

    #import "ToastAlert.h"
    #import <QuartzCore/QuartzCore.h>
    
    @implementation ToastAlert
    
    #define POPUP_DELAY  1.5
    
    - (id)initWithText: (NSString*) msg
    {
    
        self = [super init];
        if (self) {
    
            self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
            self.textColor = [UIColor colorWithWhite:1 alpha: 0.95];
            self.font = [UIFont fontWithName: @"Helvetica-Bold" size: 13];
            self.text = msg;
            self.numberOfLines = 0;
            self.textAlignment = UITextAlignmentCenter;
            self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    
    
        }
        return self;
    }
    
    - (void)didMoveToSuperview {
    
        UIView* parent = self.superview;
    
        if(parent) {
    
            CGSize maximumLabelSize = CGSizeMake(300, 200);
            CGSize expectedLabelSize = [self.text sizeWithFont: self.font  constrainedToSize:maximumLabelSize lineBreakMode: NSLineBreakByTruncatingTail];
    
            expectedLabelSize = CGSizeMake(expectedLabelSize.width + 20, expectedLabelSize.height + 10);
    
            self.frame = CGRectMake(parent.center.x - expectedLabelSize.width/2,
                                    parent.bounds.size.height-expectedLabelSize.height - 10,
                                    expectedLabelSize.width,
                                    expectedLabelSize.height);
    
            CALayer *layer = self.layer;
            layer.cornerRadius = 4.0f;
    
            [self performSelector:@selector(dismiss:) withObject:nil afterDelay:POPUP_DELAY];
        }
    }
    
    - (void)dismiss:(id)sender {
        // Fade out the message and destroy self
        [UIView animateWithDuration:0.6  delay:0 options: UIViewAnimationOptionAllowUserInteraction
                         animations:^  { self.alpha = 0; }
                         completion:^ (BOOL finished) { [self removeFromSuperview]; }];
    }
    
    @end
    
        6
  •  4
  •   David    14 年前

    我最终创建了自己的班级。未从UIAlertView继承。一般结构是,

    -(id)initWithText:(NSString *)msg {
        // Create a view. Put a label, set the msg
        CALayer *layer = self.layer;
        layer.cornerRadius = 8.0f;
        ...
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
        [self performSelector:@selector(dismiss:) withObject:nil afterDelay:2.0];
        [self setAutoresizesSubviews:FALSE];
        return self;
    }
    
    
    - (void)dismiss:(id)sender {
        // Fade out the message and destroy self
        [UIView animateWithDuration:0.5 
                     animations:^  { self.alpha = 0; }
                     completion:^ (BOOL finished) { [self removeFromSuperview]; }];
    }
    
        7
  •  2
  •   theory    12 年前

    类似于@marco mustapic的答案,但没有继承权。

    - (void)dismissAlert:(UIAlertView *)alertView
    {
        [alertView dismissWithClickedButtonIndex:0 animated:YES];
    }
    
    - (void)showPopupWithTitle:(NSString *)title
                        mesage:(NSString *)message
                  dismissAfter:(NSTimeInterval)interval
    {
        UIAlertView *alertView = [[UIAlertView alloc]
               initWithTitle:title
                     message:message
                    delegate:nil
            cancelButtonTitle:nil
            otherButtonTitles:nil
        ];
        [alertView show];
        [self performSelector:@selector(dismissAlert:)
                   withObject:alertView
                   afterDelay:interval
        ];
    }
    

    使用它:

    [self showPopupWithTitle:@"Hi" message:@"I like pie" dismissAfter:2.0];
    

    把它放到nsObject或其他东西上的一个类别中,以便始终拥有它。

        8
  •  2
  •   Idan    9 年前

    *Swift 2.2答案:

    func showPopupWithTitle(title: String, message: String, interval: NSTimeInterval) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        presentViewController(alertController, animated: true, completion: nil)
        self.performSelector(#selector(dismissAlertViewController), withObject: alertController, afterDelay: interval)
    }
    
    func dismissAlertViewController(alertController: UIAlertController) {
        alertController.dismissViewControllerAnimated(true, completion: nil)
    }
    
    showPopupWithTitle("Title", message: "Message", interval: 0.5)
    
        9
  •  2
  •   Micah Montoya    9 年前

    这只是一个 斯威夫特3版 属于 用户223 2.2版。

    func showPopupWithTitle(title: String, message: String, interval: TimeInterval) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        present(alertController, animated: true, completion: nil)
        self.perform(#selector(dismissAlertViewController), with: alertController, afterDelay: interval)
    }
    
    func dismissAlertViewController(alertController: UIAlertController) {
        alertController.dismiss(animated: true, completion: nil)
    }
    showPopupWithTitle(title: "Title", message: "Message", interval: 0.5)
    
        10
  •  1
  •   LowKostKustomz    8 年前

    您可以使用我自己的 statusalert framework written in swift.它使您能够在 uiview>中的任何位置显示类似Apple系统的警报,以及显示相同的警报而不显示图像、标题或消息。

    它通过Cocoapods和Carthage提供,支持iPhone X、安全区域布局、iPad,并允许一些定制。

    显示类似于警报的Apple系统,并且在任何位置显示相同的警报而不显示图像、标题或消息。 UIView .

    它通过Cocoapods和迦太基提供,支持iPhoneX、安全区域布局、iPad,并允许一些定制。

    StatusAlertExample application screenshow

    推荐文章