当我编写 Android 应用时,我喜欢 Toast 功能。在使用 MonoTouch (C# .NET) 进行 iPhone 开发的过程中,是否有一种类似的“设置并忘记”的弹出消息方式呢?
当我编写 Android 应用时,我喜欢 Toast 功能。在使用 MonoTouch (C# .NET) 进行 iPhone 开发的过程中,是否有一种类似的“设置并忘记”的弹出消息方式呢?
这里是MonoTouch Toast版本。受安卓启发而来。
要调用它,
ToastView t = new ToastView ("Email Sent", 1000);
t.Show ();
文件枚举:
public enum ToastGravity
{
Top = 0,
Bottom = 1,
Center = 2
}
提示设置文件:
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;
}
}
}
主要的 Toast 类:
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 ();
}
}
}
[self.view makeToast:@"This is some message as toast."
duration:3.0
position:@"bottom"];
UIAlertView
的东西?[[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];
你可能需要本地通知,它们可以让你设置一个时间,在 epoch 时间中触发。不过我认为没有办法隐藏它们。但是,如果我对 Toast 不熟悉,可能会误解你的问题。
你可以使用以下代码与uilabel和uianimation一起使用,以获得类似于Android的toast。它执行两项任务,一项是toast任务,另一项是根据文本长度和自动换行增加标签高度,在IOS 7之后这里放链接。
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];
}];
Toast.h
@interface Toast : NSObject
+ (void)toast:(NSString *)message
:(UIView *) view
:(int)delay;
@end
Toast.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
我真的很喜欢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);
}
BeginInvokeOnMainThread(() =>
{
ShowToast(message);
});