android.widget.toast,什么是适用于iOS应用程序的android.widget.Toast?

什么是适用于iOS应用程序的android.widget.Toast?

几个月前,我已经制作了Android应用程序。 吐司课程对我来说非常有用。我不需要考虑主线程和显示它的位置。 我可以显示它的任何地方,只要离开它,它就会自动消失。

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

而已。 ^^

iPhone呢? 有吐司吗? 只需显示消息即可,无需关心。 它将自动消失。

mooongcle asked 2020-01-22T19:03:09Z

6个解决方案

72 votes

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

ToastView.h

#import

@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];

SSemashko answered 2020-01-22T19:04:04Z

4 votes

UIKit中没有“开箱即用”的类可以执行此操作。 但是创建一个可以提供这种行为的类非常容易。

您只需要创建一个从UIView继承的类。 这个班有责任  -创建您要显示的内容,  -将自己添加到父视图层次结构中  -使用计时器解散自己。

您将可以像这样使用它:

[ToastView toastViewInView:myParentView withText:@"what a wonderful text"];

问候,昆汀

Quentin answered 2020-01-22T19:03:31Z

2 votes

尚未尝试,但您可能要检查:

[https://github.com/ecstasy2/toast-notifications-ios]

[https://github.com/scalessec/Toast]

kitimenpolku answered 2020-01-22T19:04:32Z

2 votes

编辑:更新为Swift 3

这是基于wojciech_maciejewski的答案的Swift 3版本。 这看起来更像Android Toast,并且不会相互堆叠Toast。 它将吐司面包吸引到屏幕中央。 它可以处理长的多行文本。

import UIKit

class ToastView: UIView {

private static let hLabelGap: CGFloat = 40.0

private static let vLabelGap: CGFloat = 20.0

private static let hToastGap: CGFloat = 20.0

private static let vToastGap: CGFloat = 10.0

private var textLabel: UILabel!

static func showInParent(_ parentView: UIView, _ text: String, duration: Double = 3.0) {

let labelFrame = CGRect(x: parentView.frame.origin.x + hLabelGap,

y: parentView.frame.origin.y + vLabelGap,

width: parentView.frame.width - 2 * hLabelGap,

height: parentView.frame.height - 2 * vLabelGap)

let label = UILabel()

label.font = UIFont.systemFont(ofSize: 15.0)

label.text = text

label.backgroundColor = UIColor.clear

label.textAlignment = NSTextAlignment.center

label.textColor = UIColor.white

label.numberOfLines = 0

label.frame = labelFrame

label.sizeToFit()

let toast = ToastView()

toast.textLabel = label

toast.addSubview(label)

toast.frame = CGRect(x: label.frame.origin.x - hToastGap,

y: label.frame.origin.y - vToastGap,

width: label.frame.width + 2 * hToastGap,

height: label.frame.height + 2 * vToastGap)

toast.backgroundColor = UIColor.darkGray

toast.alpha = 0.0

toast.layer.cornerRadius = 20.0

toast.center = parentView.center

label.center = CGPoint(x: toast.frame.size.width / 2, y: toast.frame.size.height / 2)

parentView.addSubview(toast)

UIView.animate(withDuration: 0.4, animations: {

toast.alpha = 0.9

label.alpha = 0.9

})

toast.perform(#selector(hideSelf), with: nil, afterDelay: duration)

}

@objc private func hideSelf() {

UIView.animate(withDuration: 0.4, animations: {

self.alpha = 0.0

self.textLabel.alpha = 0.0

}, completion: { t in self.removeFromSuperview() })

}

}

来自另一个控制器的用法:

ToastView.showInParent(navigationController!.view, "Hello world")

petrsyn answered 2020-01-22T19:05:01Z

1 votes

我发布了Scarmysun的快速版本答案:)非常感谢

import Foundation

import UIKit

class ToastView: UIView {

static let toastHeight:CGFloat = 50.0

static let toastGap:CGFloat = 10;

lazy var textLabel: UILabel = UILabel(frame: CGRectMake(5.0, 5.0, self.frame.size.width - 10.0, self.frame.size.height - 10.0))

static func showInParent(parentView: UIView!, withText text: String, forDuration duration: double_t) {

//Count toast views are already showing on parent. Made to show several toasts one above another

var toastsAlreadyInParent = 0;

for view in parentView.subviews {

if (view.isKindOfClass(ToastView)) {

toastsAlreadyInParent++

}

}

var parentFrame = parentView.frame;

var yOrigin = parentFrame.size.height - getDouble(toastsAlreadyInParent)

var selfFrame = CGRectMake(parentFrame.origin.x + 20.0, yOrigin, parentFrame.size.width - 40.0, toastHeight);

var toast = ToastView(frame: selfFrame)

toast.textLabel.backgroundColor = UIColor.clearColor()

toast.textLabel.textAlignment = NSTextAlignment.Center

toast.textLabel.textColor = UIColor.whiteColor()

toast.textLabel.numberOfLines = 2

toast.textLabel.font = UIFont.systemFontOfSize(13.0)

toast.addSubview(toast.textLabel)

toast.backgroundColor = UIColor.darkGrayColor()

toast.alpha = 0.0;

toast.layer.cornerRadius = 4.0;

toast.textLabel.text = text;

parentView.addSubview(toast)

UIView.animateWithDuration(0.4, animations: {

toast.alpha = 0.9

toast.textLabel.alpha = 0.9

})

toast.performSelector(Selector("hideSelf"), withObject: nil, afterDelay: duration)

}

static private func getDouble(toastsAlreadyInParent : Int) -> CGFloat {

return (70.0 + toastHeight * CGFloat(toastsAlreadyInParent) + toastGap * CGFloat(toastsAlreadyInParent));

}

func hideSelf() {

UIView.animateWithDuration(0.4, animations: {

self.alpha = 0.0

self.textLabel.alpha = 0.0

}, completion: { t in self.removeFromSuperview() })

}

}

wojciech_maciejewski answered 2020-01-22T19:05:21Z

1 votes

您可以通过多种方式执行此操作,一种方法是在swift3中使用UIAlertViewController()

let alertManager=UIAlertController(title: nil, message: "Welcome!", preferredStyle: .alert)

self.present(alertManager, animated: true, completion: nil)

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1,

execute: {

alertManager.dismiss(animated: false, completion: nil)

})

Mahesh Giri answered 2020-01-22T19:05:41Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值