ios - iOS 应用程序的 android.widget.Toast 等价物是什么?

标签 ios toast

我在几个月前制作了 Android 应用程序。 Toast 类对我来说非常有用。 我不需要考虑主线程和显示它的地方。任何我可以展示它的地方,只要留下它,它就会自动消失。

Toast.makeToast(context, msg, Toast.LENGTH_SHORT).show();

就是这样。 ^^

iPhone 呢?有没有类似 Toast 的东西?只显示消息,不需要关心它。它会自动消失。

最佳答案

我已经为 Android 编写了很长时间,但我很想念 Toast。我已经实现了一个。需要代码?你在这里:

ToastView.h

#import <UIKit/UIKit.h>

@interface ToastView : UIView

@property (strong, nonatomic) NSString *text;

+ (void)showToastInParentView: (UIView *)parentView withText:(NSString *)text withDuaration:(float)duration;

@end

ToastView.m

#import "ToastView.h"

@interface ToastView ()
@property (strong, nonatomic, readonly) UILabel *textLabel;
@end
@implementation ToastView
@synthesize textLabel = _textLabel;

float const ToastHeight = 50.0f;
float const ToastGap = 10.0f;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(UILabel *)textLabel
{
    if (!_textLabel) {
        _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, self.frame.size.width - 10.0, self.frame.size.height - 10.0)];
        _textLabel.backgroundColor = [UIColor clearColor];
        _textLabel.textAlignment = NSTextAlignmentCenter;
        _textLabel.textColor = [UIColor whiteColor];
        _textLabel.numberOfLines = 2;
        _textLabel.font = [UIFont systemFontOfSize:13.0];
        _textLabel.lineBreakMode = NSLineBreakByCharWrapping;
        [self addSubview:_textLabel];

    }
    return _textLabel;
}

- (void)setText:(NSString *)text
{
    _text = text;
    self.textLabel.text = text;
}

+ (void)showToastInParentView: (UIView *)parentView withText:(NSString *)text withDuaration:(float)duration;
{

    //Count toast views are already showing on parent. Made to show several toasts one above another
    int toastsAlreadyInParent = 0;
    for (UIView *subView in [parentView subviews]) {
        if ([subView isKindOfClass:[ToastView class]])
        {
            toastsAlreadyInParent++;
        }
    }

    CGRect parentFrame = parentView.frame;

    float yOrigin = parentFrame.size.height - (70.0 + ToastHeight * toastsAlreadyInParent + ToastGap * toastsAlreadyInParent);

    CGRect selfFrame = CGRectMake(parentFrame.origin.x + 20.0, yOrigin, parentFrame.size.width - 40.0, ToastHeight);
    ToastView *toast = [[ToastView alloc] initWithFrame:selfFrame];

    toast.backgroundColor = [UIColor darkGrayColor];
    toast.alpha = 0.0f;
    toast.layer.cornerRadius = 4.0;
    toast.text = text;

    [parentView addSubview:toast];

    [UIView animateWithDuration:0.4 animations:^{
        toast.alpha = 0.9f;
        toast.textLabel.alpha = 0.9f;
    }completion:^(BOOL finished) {
        if(finished){

        }
    }];


    [toast performSelector:@selector(hideSelf) withObject:nil afterDelay:duration];

}

- (void)hideSelf
{

    [UIView animateWithDuration:0.4 animations:^{
        self.alpha = 0.0;
        self.textLabel.alpha = 0.0;
    }completion:^(BOOL finished) {
        if(finished){
            [self removeFromSuperview];
        }
    }];
}

@end

从 ViewController 调用

 [ToastView showToastInParentView:self.view withText:@"What a toast!" withDuaration:5.0];

关于ios - iOS 应用程序的 android.widget.Toast 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3522866/

相关文章:

ios - 如何在不阻止付费客户继续使用它的情况下将我的应用从应用商店下架?

ios - 核心数据一对多关系

angular - 在primeng p-toast中使用html

ios - show Question 显示问题(null)而不是存储在数组中的问题

ios - Swift & ObjC 桥 - 找不到 "WKNavigationDelegate"的协议(protocol)声明

java - 在 android 中使用 intent 发送短信后不显示 toast

java - 无法显示库的 JFrame

java - 如何从底部始终保持 30dpi 的 toast ?

ios - 通过 Swift 中的触摸检测应用程序何时处于事件状态或非事件状态

typescript - 如何使用 primeVue toast 创建可重用的 toastService?