Ian*_*ink 12 iphone android xamarin.ios ipad ios
当我编写Android应用程序时,我喜欢Toast功能.有没有办法在使用MonoTouch(C#.NET)的iPhone开发中获得这种设置并忘记弹出消息?
Ian*_*ink 14
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 ();
        }
    }
}
看一下这个:
https://github.com/ecstasy2/toast-notifications-ios
编辑:项目已移至github所以我更新链接.
| 归档时间: | 
 | 
| 查看次数: | 16377 次 | 
| 最近记录: |