2010-11-19 120 views

回答

9

检查了这一点:

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

编辑:该项目已经转移到GitHub上,所以我更新的链接。

+0

我可以在siumulator上测试它吗?似乎没有为我工作。我在我的控制器的viewDidLoad方法中创建了一个toast。 – 2011-02-22 14:42:09

+0

它在模拟器上工作。你可以通过示例代码吗? – clide313 2011-02-22 19:29:48

+1

这真的很不错,但是LGPL授权,所以你不能在iPhone应用程序中使用它,而不会使你的所有源代码都在LGPL之下。它与iPhone平时不同的原因是iPhone使用静态库.. – 2011-03-15 18:59:14

2

你在找什么类似UIAlertView

+0

不,这需要一个计时器来显示和隐藏。更简单一些,也许是开源社区的一个罐头类。敬酒内部照顾更多的管道。 – 2010-11-19 01:27:37

+0

为什么不创建一个包装警报视图和计时器的类?也许你可以开源,从而解决你的问题和其他人的问题。 – Jasarien 2010-11-19 01:32:54

+0

Jasarien是对的,你想解决的问题很简单,也很有用,敬酒摇滚。这对社区会有很大的帮助。 – blindstuff 2010-11-19 01:55:59

1

你可能会在本地通知之后,很确定他们允许你设置一个时间,我想在时间被解雇了。不要认为有办法隐藏它们。虽然我不熟悉吐司,但我可能会误解你的问题。

14

MonoTouch吐司版本在这里。受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(); 
     } 

    } 
} 
+0

好工作的家伙,你的翻译是完美的。 – clide313 2011-02-14 11:44:21

+0

在哪种类型的文件中,我们应该写这个代码? – Azhar 2011-10-14 06:20:21

+1

这是一个C#类,所以在AnyThingYouWant.cs很好 – 2011-10-18 15:46:04

-1

我在github上创建了一个新的回购库,提供一个类来执行iOS吐司式警报。我不喜欢code.google.com上的那个,它没有正确旋转并且不漂亮。

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

享受乡亲。

+0

使它依赖于巨大的Three20库使其过于庞大IMO – Roger 2012-10-13 11:17:19

+0

我已经使用它成功了一年多,但当然每个项目都应该包括你的心脏告诉你的东西。 – esilver 2012-10-13 15:20:25

4

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

我认为这是简单的使用,因为它实现为OBJ-C类,从而增加了makeToast方法的UIView的任何实例。例如:

[self.view makeToast:@"This is some message as toast." 
      duration:3.0 
      position:@"bottom"]; 
+0

有没有办法禁用父视图,以便用户不能点击远离活动吐司?我需要用户停止与应用程序交互,同时显示活动面包。 (只有活动吐司,其余不需要这种行为。) – jkcl 2012-12-12 12:40:47

2

你可以使用这个链接,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]; 
0

我已经为处理显示旋转的吐司类添加了一些修改。

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

    } 
0

只要你可以使用下面的代码与uilabel和uianimation得到像在android中的烤面包。 它做两件作品之一是烤面包的任务,它增加了标签的高度根据与换行IOS 7文本长度后link here

CGRect initialFrame = CGRectMake(20, self.view.frame.size.height/2,300, 40); 


NSString *[email protected]"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]; 
}]; 
0

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

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

我非常喜欢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); 
}); 
+0

这会阻止用户交互,直到显示Toast时,Android中的Toast并不是这种情况。 – 2016-04-15 11:02:06

+0

感谢您的评论。你会如何避免这种情况? – 2016-04-15 13:56:10