iPhone上有类似于安卓Toast的功能吗?

13

当我编写 Android 应用时,我喜欢 Toast 功能。在使用 MonoTouch (C# .NET) 进行 iPhone 开发的过程中,是否有一种类似的“设置并忘记”的弹出消息方式呢?

13个回答

15

这里是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 ();
        }

    }
}

干得好,伙计,你的翻译完美无缺。 - clide313
1
这是一个C#类,所以在AnyThingYouWant.cs文件中就可以了。 - Ian Vink
横屏视图需要我改变什么? - technomage

9

我可以在模拟器上测试吗?对我来说好像无法工作。我已经在控制器的viewDidLoad方法中创建了一个toast提示。 - Praveen S
1
这真的很好,但是它是LGPL许可的,所以如果您不将所有源代码都置于LGPL下,则无法在iPhone应用程序中使用它。 iPhone与通常不同的原因是iPhone使用静态库.. - Ben Clayton
刚刚将许可证更改为MIT。目标是允许每个人无限制地使用它。感谢您的评论。 - clide313
谢谢提供链接,非常好的库,点赞! - AL̲̳I

4
这是我的版本:http://github.com/scalessec/toast。我认为这个库的使用更加简单,因为它是作为一个 obj-c 类别实现的,可以将 makeToast 方法添加到任何 UIView 实例中。例如:
[self.view makeToast:@"This is some message as toast."
            duration:3.0
            position:@"bottom"];

有没有办法禁用父视图,以便用户无法从活动toast中点击离开?我需要在活动toast显示时停止用户与应用程序的交互。(只有活动toast需要这种行为,其他部分不需要。) - jkcl

2
你是否正在寻找类似于UIAlertView的东西?

不需要计时器来显示和隐藏。也许可以使用开源社区的现成类库,更简单些。Toast在内部处理更多的基础设施工作。 - Ian Vink
为什么不创建一个封装警告视图和计时器的类呢?也许你可以将其开源,从而解决自己的问题,同时也许能够帮到其他人。 - Jasarien
Jasarien是正确的,您想解决的问题非常简单而且也真的很有用,吐司很棒。这将对社区有很大的帮助。 - blindstuff
我接受的答案非常完美。我将把它翻译成MonoTouch,以便所有Microsoft.NET iOS开发人员都可以使用它。 - Ian Vink
谢谢评论。我觉得这是有用的东西,所以分享出来。实际上,还没有太多开发者下载它,我很快会发布一些更新,包括自定义警报主题和图标。 - clide313

2
您可以使用此链接获取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];

1

你可能需要本地通知,它们可以让你设置一个时间,在 epoch 时间中触发。不过我认为没有办法隐藏它们。但是,如果我对 Toast 不熟悉,可能会误解你的问题。


1

你可以使用以下代码与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];
}];

1
我将John的回答修改如下:

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

0

0

我真的很喜欢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 就不是这样。 - Akash Kava
感谢您的评论。您会如何避免这种情况? - Daniele D.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接