iPhone中的Android Toast?
Posted
技术标签:
【中文标题】iPhone中的Android Toast?【英文标题】:Android Toast in iPhone? 【发布时间】:2011-05-12 09:26:10 【问题描述】:当我编写 android 应用程序时,我喜欢 Toast 功能。有没有办法在使用 MonoTouch (C# .NET) 的 iPhone 开发中获得这种设置并忘记弹出消息?
【问题讨论】:
【参考方案1】:这里是 MonoTouch Toast 版本。受到 Android 的启发。
叫它,
ToastView t = new ToastView ("Email Sent", 1000);
t.Show ();
枚举文件:
public enum ToastGravity
Top = 0,
Bottom = 1,
Center = 2
ToastSettings 文件:
using System;
using System.Drawing;
using MonoTouch.UIKit;
namespace General
public class ToastSettings
public ToastSettings ()
this.Duration = 500;
this.Gravity = ToastGravity.Center;
public int Duration
get;
set;
public double DurationSeconds
get return (double) Duration/1000 ;
public ToastGravity Gravity
get;
set;
public PointF Position
get;
set;
主吐司类:
using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.ObjCRuntime;
namespace General
public class ToastView : NSObject
ToastSettings theSettings = new ToastSettings ();
private string text = null;
UIView view;
public ToastView (string Text, int durationMilliseonds)
text = Text;
theSettings.Duration = durationMilliseonds;
int offsetLeft = 0;
int offsetTop = 0;
public ToastView SetGravity (ToastGravity gravity, int OffSetLeft, int OffSetTop)
theSettings.Gravity = gravity;
offsetLeft = OffSetLeft;
offsetTop = OffSetTop;
return this;
public ToastView SetPosition (PointF Position)
theSettings.Position = Position;
return this;
public void Show ()
UIButton v = UIButton.FromType (UIButtonType.Custom);
view = v;
UIFont font = UIFont.SystemFontOfSize (16);
SizeF textSize = view.StringSize (text, font, new SizeF (280, 60));
UILabel label = new UILabel (new RectangleF (0, 0, textSize.Width + 5, textSize.Height + 5));
label.BackgroundColor = UIColor.Clear;
label.TextColor = UIColor.White;
label.Font = font;
label.Text = text;
label.Lines = 0;
label.ShadowColor = UIColor.DarkGray;
label.ShadowOffset = new SizeF (1, 1);
v.Frame = new RectangleF (0, 0, textSize.Width + 10, textSize.Height + 10);
label.Center = new PointF (v.Frame.Size.Width / 2, v.Frame.Height / 2);
v.AddSubview (label);
v.BackgroundColor = UIColor.FromRGBA (0, 0, 0, 0.7f);
v.Layer.CornerRadius = 5;
UIWindow window = UIApplication.SharedApplication.Windows[0];
PointF point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height / 2);
if (theSettings.Gravity == ToastGravity.Top)
point = new PointF (window.Frame.Size.Width / 2, 45);
else if (theSettings.Gravity == ToastGravity.Bottom)
point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height - 45);
else if (theSettings.Gravity == ToastGravity.Center)
point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height / 2);
else
point = theSettings.Position;
point = new PointF (point.X + offsetLeft, point.Y + offsetTop);
v.Center = point;
window.AddSubview (v);
v.AllTouchEvents += delegate HideToast (null); ;
NSTimer.CreateScheduledTimer (theSettings.DurationSeconds, HideToast);
void HideToast ()
UIView.BeginAnimations ("");
view.Alpha = 0;
UIView.CommitAnimations ();
void RemoveToast ()
view.RemoveFromSuperview ();
【讨论】:
干得好,你的翻译很完美。 这是一个 C# 类,所以在 AnyThingYouWant.cs 中就可以了 横向视图需要更改哪些内容?【参考方案2】:看看这个:
https://github.com/ecstasy2/toast-notifications-ios
编辑:该项目已移至 github,因此我更新了链接。
【讨论】:
我可以在模拟器上测试吗?似乎对我不起作用。我在控制器的 viewDidLoad 方法中创建了一个 toast。 这真的很好,但它是 LGPL 许可的,所以你不能在 iPhone 应用程序中使用它,除非你的所有源代码也都在 LGPL 之下。 iPhone 上与平常不同的原因是 iPhone 使用静态库 .. 刚刚将许可证更改为 MIT。目标是让每个人都可以不受限制地使用它。非常感谢您的评论。 感谢您的链接.. 非常好的图书馆.. +1【参考方案3】:这是我的版本:http://github.com/scalessec/toast
我认为它使用起来更简单,因为它被实现为 obj-c 类别,从而将 makeToast 方法添加到 UIView 的任何实例。例如:
[self.view makeToast:@"This is some message as toast."
duration:3.0
position:@"bottom"];
【讨论】:
有没有办法禁用父视图,这样用户就不能点击离开活动吐司?我需要用户在显示活动吐司时停止与应用程序交互。 (只有活动吐司,其余的不需要这种行为。)【参考方案4】:您在寻找类似UIAlertView
的东西吗?
【讨论】:
不,这需要一个计时器来显示和隐藏。一些更简单的东西,也许是来自开源社区的罐头类。 Toast 在内部负责更多的管道。 为什么不创建一个包含警报视图和计时器的类呢?也许您可以将其开源,从而解决您的问题,也可能解决其他人的问题。 Jasarien 是对的,你要解决的问题很简单,也很实用,toasts rock。这对社区会有很大帮助。 我接受的答案非常适合这个。我会将它翻译成 MonoTouch,以便所有 Microsoft.NET iOS 开发人员都可以使用它。 感谢 cmets。我认为这是有用的东西所以分享它。实际上还没有多少开发者下载它,我很快就会发布一些更新,包括自定义警报主题+图标。【参考方案5】:您可以使用此链接获取 Toast 的 Objective-c 代码
http://code.google.com/p/toast-notifications-ios/source/browse/trunk/
此链接为它的用法
http://code.google.com/p/toast-notifications-ios/wiki/HowToUse
可能类似于以下任何一个示例
[[iToast makeText:NSLocalizedString(@"The activity has been successfully saved.", @"")] show];
[[[iToast makeText:NSLocalizedString(@"The activity has been successfully saved.", @"")]
setGravity:iToastGravityBottom] show];
[[[[iToast makeText:NSLocalizedString(@"Something to display a very long time", @"")]
etGravity:iToastGravityBottom] setDuration:iToastDurationLong] show];
【讨论】:
【参考方案6】:您可能会在本地通知之后,很确定它们允许您设置时间,我认为在纪元时间内被解雇。不要认为有办法隐藏它们。我可能误解了你的问题,因为我不熟悉 Toast。
【讨论】:
【参考方案7】:只是您可以使用以下代码与 uilabel 和 uianimation 来获得像在 android 中一样的 toast。 它做了两项工作,一项是 toast 任务,它根据文本长度增加标签的高度,稍后使用 wordwrap IOS 7 link here
CGRect initialFrame = CGRectMake(20, self.view.frame.size.height/2,300, 40);
NSString *message=@"Toast in Iphone as in Android";
UILabel *flashLabel=[[UILabel alloc] initWithFrame:initialFrame];
flashLabel.font=[UIFont fontWithName:@"Optima-Italic" size:12.0];
flashLabel.backgroundColor=[UIColor whiteColor];
flashLabel.layer.cornerRadius=3.0f;
flashLabel.numberOfLines=0;
flashLabel.textAlignment=NSTextAlignmentCenter;
CGSize maxSize = CGSizeMake(flashLabel.frame.size.width, MAXFLOAT);
CGRect labelRect = [message boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@NSFontAttributeName:flashLabel.font context:nil];
//adjust the label the the new height.
CGRect newFrame = flashLabel.frame;
newFrame.size.height = labelRect.size.height;
flashLabel.frame = newFrame;
flashLabel.text=message;
[self.view addSubview:flashLabel];
flashLabel.alpha=1.0;
self.view.userInteractionEnabled=FALSE;
[UIView animateWithDuration:13.0 animations:^
flashLabel.alpha=0.0f;
completion:^(BOOL finished)
self.view.userInteractionEnabled=TRUE;
[flashLabel removeFromSuperview];
];
【讨论】:
【参考方案8】:我将约翰的回答修改如下:
吐司.h
@interface Toast : NSObject
+ (void)toast:(NSString *)message
:(UIView *) view
:(int)delay;
@end
吐司.m
#import "Toast.h"
@interface Toast ()
@end
@implementation Toast
+ (void)toast:(NSString *)message
:(UIView *) view
:(int)delay
CGRect initialFrame = CGRectMake(10, view.frame.size.height/2, 300, 40);
UILabel *flashLabel=[[UILabel alloc] initWithFrame:initialFrame];
flashLabel.font=[UIFont fontWithName:@"Optima-Italic" size:19.0];
flashLabel.backgroundColor=[UIColor whiteColor];
flashLabel.layer.cornerRadius=9.0f;
flashLabel.clipsToBounds = YES;
flashLabel.numberOfLines=3;
flashLabel.textAlignment=NSTextAlignmentCenter;
CGSize maxSize = CGSizeMake(flashLabel.frame.size.width, MAXFLOAT);
CGRect labelRect = [message boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@NSFontAttributeName:flashLabel.font
context:nil];
//adjust the label the the new height.
CGRect newFrame = flashLabel.frame;
newFrame.size.height = labelRect.size.height * 2;
flashLabel.frame = newFrame;
flashLabel.text=message;
[view addSubview:flashLabel];
flashLabel.alpha=1.0;
view.userInteractionEnabled=FALSE;
[UIView animateWithDuration:delay animations:^
flashLabel.alpha=0.0f;
completion:^(BOOL finished)
view.userInteractionEnabled=TRUE;
[flashLabel removeFromSuperview];
];
@end
【讨论】:
【参考方案9】:我对处理显示旋转的 toast 类添加了一些修改。
public void Show ()
UIButton v = UIButton.FromType (UIButtonType.Custom);
view = v;
UIFont font = UIFont.SystemFontOfSize (16);
SizeF textSize = view.StringSize (text, font, new SizeF (280, 60));
UILabel label = new UILabel (new RectangleF (0, 0, textSize.Width + 5, textSize.Height + 5));
label.BackgroundColor = UIColor.Clear;
label.TextColor = UIColor.White;
label.Font = font;
label.Text = text;
label.Lines = 0;
label.ShadowColor = UIColor.DarkGray;
label.ShadowOffset = new SizeF (1, 1);
v.Frame = new RectangleF (0, 0, textSize.Width + 10, textSize.Height + 10);
label.Center = new PointF (v.Frame.Size.Width / 2, v.Frame.Height / 2);
v.AddSubview (label);
v.BackgroundColor = UIColor.FromRGBA (0, 0, 0, 0.7f);
v.Layer.CornerRadius = 5;
UIWindow window = UIApplication.SharedApplication.Windows[0];
PointF point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height / 2);
if (theSettings.Gravity == ToastGravity.Top)
point = new PointF (window.Frame.Size.Width / 2, 45);
else if (theSettings.Gravity == ToastGravity.Bottom)
point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height - 45);
else if (theSettings.Gravity == ToastGravity.Center)
point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height / 2);
else
point = theSettings.Position;
point = new PointF (point.X + offsetLeft, point.Y + offsetTop);
v.Center = point;
//handle screen rotation
float orientation=0;
switch(UIApplication.SharedApplication.StatusBarOrientation)
case UIInterfaceOrientation.LandscapeLeft:
orientation=-90;
break;
case UIInterfaceOrientation.LandscapeRight:
orientation=90;
break;
case UIInterfaceOrientation.PortraitUpsideDown:
orientation=180;
break;
v.Transform=CGAffineTransform.MakeRotation ((float)(orientation / 180f * Math.Pi));
window.AddSubview (v);
v.AllTouchEvents += delegate HideToast (); ;
NSTimer.CreateScheduledTimer (theSettings.DurationSeconds, HideToast);
【讨论】:
【参考方案10】:你可以试试我的开源库 TSMessages:https://github.com/toursprung/TSMessages
在 iOS 5/6 和 iOS 7 上,它真的很容易使用并且看起来很漂亮。
【讨论】:
【参考方案11】:我非常喜欢 Bahai 提出的 MonoTouch 解决方案。
以下不是替代品。只是一种对我有用的现成方法。
private async Task ShowToast(string message, UIAlertView toast = null)
if (null == toast)
toast = new UIAlertView(null, message, null, null, null);
toast.Show();
await Task.Delay(2000);
await ShowToast(message, toast);
return;
UIView.BeginAnimations("");
toast.Alpha = 0;
UIView.CommitAnimations();
toast.DismissWithClickedButtonIndex(0, true);
如果该方法是从后台线程(不是主 UI 线程)调用的,则需要 BeginInvokeOnMainThread,这意味着只需像这样调用它。
BeginInvokeOnMainThread(() =>
ShowToast(message);
);
【讨论】:
这会在显示 Toast 之前阻止用户交互,Android 中的 Toast 不是这种情况。 感谢您的评论。您将如何避免这种情况?【参考方案12】:我在 github 上创建了一个新的存储库,其中包含一个用于执行 iOS toast 样式警报的类。我不喜欢 code.google.com 上的那个,它没有正确旋转并且不漂亮。
https://github.com/esilverberg/ios-toast
尽情享受吧。
【讨论】:
使其依赖于庞大的 Three20 库使其过于重量级 IMO 我已经成功使用了一年多,当然每个项目都应该包含你的心声。以上是关于iPhone中的Android Toast?的主要内容,如果未能解决你的问题,请参考以下文章