代码之家  ›  专栏  ›  技术社区  ›  Ian Vink

Android Toast在iPhone中?

  •  12
  • Ian Vink  · 技术社区  · 14 年前

    当我写Android应用程序时,我喜欢Toast特性。在使用MonoTouch(C#.NET)开发iPhone时,有没有一种方法可以获得这种设置并忘记弹出消息?

    12 回复  |  直到 11 年前
        1
  •  9
  •   clide313    11 年前

    看看这个:

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

    编辑:项目已移动到github,因此我更新了链接。

        2
  •  14
  •   miguel.de.icaza    13 年前

    单触式吐司版本。灵感来自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 ();
            }
    
        }
    }
    
        3
  •  4
  •   user2393462435    13 年前

    这是我的版本: http://github.com/scalessec/toast

    我认为它使用起来更简单,因为它是作为obj-c类别实现的,因此将makeToast方法添加到UIView的任何实例中。如:

    [self.view makeToast:@"This is some message as toast."
                duration:3.0
                position:@"bottom"];
    
        4
  •  2
  •   Jasarien    14 年前

    你在找什么 UIAlertView ?

        5
  •  2
  •   Azhar    13 年前

    您可以使用此链接获取objective-c Toast代码

    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
  •  1
  •   Rudiger    14 年前

    你可能是在本地通知之后,很确定他们允许你设置一个时间,我想在纪元时间被炒鱿鱼。不过,别以为有办法把它们藏起来。不过,我可能会误解你的问题,因为我不熟悉吐司。

        7
  •  1
  •   John    10 年前

    只需将下面的代码与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
  •  1
  •   Lee Hounshell    10 年前

    我修改了约翰的回答如下:

    烤面包.h

    @interface Toast : NSObject
    
    + (void)toast:(NSString *)message
                 :(UIView *) view
                 :(int)delay;
    
    @end
    

    吐司

    #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
  •  0
  •   Bob Powell    11 年前

    我对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
  •  0
  •   KrauseFx    11 年前

    您可以尝试我的开源库TSMessages: https://github.com/toursprung/TSMessages

    它非常容易使用,在iOS5/6和iOS7上看起来也很漂亮。

        11
  •  0
  •   Daniele D.    8 年前

    我真的很喜欢巴哈伊提出的单点触控解决方案。

    以下不是替换。对我来说只是一种现成的方法。

        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);
    });
    
        12
  •  -1
  •   esilver rmaddy    13 年前

    我在github上创建了一个新的repo,该repo带有一个类来执行iOS toast风格的警报。我不喜欢code.google.com上的那个,它旋转不正常,不漂亮。

    https://github.com/esilverberg/ios-toast

    祝大家愉快。