Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

ios - Android Toast in iPhone?

When I write Android apps, I love the Toast feature. Is there a way to get this kind of set and forget popup message in iPhone development using MonoTouch (C# .NET)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

MonoTouch Toast Version here. Inspired by Android.

To call it,

        ToastView t = new ToastView ("Email Sent", 1000);
        t.Show ();

Enum File:

public enum ToastGravity
{
    Top = 0,
    Bottom = 1,
    Center = 2
}

ToastSettings File:

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;
        }


    }
}

Main Toast Class:

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

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...