【问题标题】:Growl/toast style notifications library for iOS适用于 iOS 的 Growl/toast 样式通知库
【发布时间】:2011-05-17 04:32:30
【问题描述】:

谁能推荐一个在 iOS 上实现咆哮或吐司式通知的库?例如,用户保存配置文件后,我想让通知淡入,停留 3 秒,报告“配置文件已保存”,然后淡出。现在我有一个 UIAlertView,它通过一个“确定”按钮中断用户的工作流程,我觉得这有点过头了。

Android Toast class 是我在 iOS 上寻找的一个示例。

谢谢!

【问题讨论】:

标签: iphone ios notifications toast


【解决方案1】:

我创建了一个我认为您会发现有用的解决方案: https://github.com/scalessec/toast

它被写成一个 obj-c 类别,本质上是向 UIView 的任何实例添加 makeToast 方法。例如:

[self.view makeToast:@"Profile saved"
            duration:2.0
            position:@"bottom"];

【讨论】:

  • 能否请您指导一下,如何将此 toast 添加到 Window 而不是 self.view??
  • [self.view.window makeToast:@"Hello world"];或 [[UIApplication sharedApplication].keyWindow makeToast:@"Hello world"];但一般来说,直接向窗口添加视图是不好的做法。如果您使用的是导航控制器,也许您真正想要的是: [self.navigationController.view makeToast:@"Hello World"];
【解决方案2】:

我是这样解决的:

  1. 在您的视图上创建通用标签。让它全屏宽,给它你需要的大小并将文本居中。
  2. 将其位置设置为“顶部” - 此标签必须位于控件列表中所有控件的下方。
  3. 将其添加到界面、属性、合成中(我们称其为“toastLabel”)。
  4. 在您的 XIB 文件中与“toastLabel”相关联
  5. 在您的 viewWillAppear 中添加以下行以隐藏开始标签:

    [toastLabel setHidden:TRUE];
    
  6. 在按钮单击(或其他一些事件)上添加以下代码:

    toastLabel.text = @"Our toast text";
    [toastLabel setHidden:TRUE];
    [toastLabel setAlpha:1.0];
    CGPoint location;
    location.x = 160; 
    location.y = 220; 
    toastLabel.center = location;
    location.x = 160; 
    location.y = 320; 
    [toastLabel setHidden:FALSE];
    [UIView animateWithDuration:0.9 animations:^{
        toastLabel.alpha = 0.0;
        toastLabel.center = location;
    }];
    

这个标签会“掉下来”消失。

【讨论】:

    【解决方案3】:

    虽然有点晚了,但这是我的看法:

    https://github.com/pcperini/PCToastMessage

    【讨论】:

      【解决方案4】:

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

      在 iOS 5/6 和 iOS 7 上,它真的很容易使用并且看起来很漂亮。

      【讨论】:

      • 我试用了这个库,屏幕截图看起来非常漂亮。但不幸的是,正如您的 wiki 上所提到的,即使在打开 Example.xcworkspace 之后,我也无法运行该项目。主要是因为我在示例项目中得到了错误 libPods.a。我真的很喜欢屏幕截图的外观,并且很想使用这个库。任何帮助表示赞赏。谢谢。
      • 对不起,我没有看到问题,我只是从master分支下载了最新版本的zip,打开Example.xcworkspace并编译它。
      • 抱歉,之前没有说清楚,感谢您的回复。我是 iPhone 开发的初学者。我是否需要设置和配置可可豆荚才能运行示例?同样在运行工作区文件之前,我直接打开了 Example.xcodeproj 文件。这可能是问题所在。
      • 你不需要 Cocoapods,但我建议在包含第三方库时使用它。
      • 再次感谢您。我能够使用下载的 zip 让它工作。我尝试了 Cocoapods,但我可能做错了,因为我的 pods.xconfig 似乎无法正常工作,因为 Xcode 无法从 pod 中找到头文件。但是,是的,我会按照您的建议学习并尝试使用 pod。
      【解决方案5】:

      我自己做的。 Krishnan 链接的班级很丑陋,并且没有正确旋转。

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

      它是这样的:

      【讨论】:

        【解决方案6】:

        我是这样弄的:

        + (void)showToastMessage:(NSString *)message {    
        UIAlertView *toast = [[UIAlertView alloc] initWithTitle:nil
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:nil
                                              otherButtonTitles:nil, nil];
        [toast show];
        
        // duration in seconds
        int duration = 2;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            [toast dismissWithClickedButtonIndex:0 animated:YES];
        });
        

        }

        iOS9+ 的更新解决方案:

        + (void)showToastMessage:(NSString *)message
                        root:(id)view {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                                     message:message
                                                                              preferredStyle:UIAlertControllerStyleAlert];
        // duration in seconds
        int duration = 2;
        [view presentViewController:alertController animated:YES completion:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            [alertController dismissViewControllerAnimated:YES completion:nil];
        });
        }
        

        【讨论】:

        • 我喜欢这个解决方案:在 Xamarin ios 中,我使用了等效项(仅 1 行):对于使用 Xamarin.IOS 的用户,您可以这样做:new UIAlertView(null, message, null, " OK", null).Show();使用 UIKit;是必需的。
        【解决方案7】:

        【讨论】:

        • 是的,v0.5 及更高版本具有纯文本模式。
        【解决方案8】:

        嘿,你正在寻找这个。

        https://github.com/PaulSolt/WEPopover#readme

        【讨论】:

        • 不,我不想要弹出框,我想要敬酒。不过还是谢谢。
        • 您可以使用该链接中的代码自己创建一个。
        • 很好的建议。虽然这不是这个问题的答案。但这对我来说似乎很有用。谢谢。
        【解决方案9】:

        这正是你想要的。 这个也很方便,因为它有一个完成块,请看一下:) https://github.com/PrajeetShrestha/EkToast

        【讨论】:

          【解决方案10】:

          Swift 2.0:

          我们的想法是制定一个对 CocoaPods 零依赖的 Toast 类。

          参考:https://github.com/scalessec/Toast-Swift

          制作一个空的 Swift 文件(file-New-File- Empty Swift File - 将其命名为 Toast。)

          添加以下代码。

          //  Toast.swift
          
          import UIKit
          import ObjectiveC
          
          enum ToastPosition {
           case Top
           case Center
           case Bottom
          }
          
          extension UIView {
          
           private struct ToastKeys {
            static var Timer        = "CSToastTimerKey"
            static var Duration     = "CSToastDurationKey"
            static var Position     = "CSToastPositionKey"
            static var Completion   = "CSToastCompletionKey"
            static var ActiveToast  = "CSToastActiveToastKey"
            static var ActivityView = "CSToastActivityViewKey"
            static var Queue        = "CSToastQueueKey"
           }
          
           private class ToastCompletionWrapper {
            var completion: ((Bool) -> Void)?
          
            init(_ completion: ((Bool) -> Void)?) {
             self.completion = completion
            }
           }
          
           private enum ToastError: ErrorType {
            case InsufficientData
           }
          
           private var queue: NSMutableArray {
            get {
             if let queue = objc_getAssociatedObject(self, &ToastKeys.Queue) as? NSMutableArray {
              return queue
             } else {
              let queue = NSMutableArray()
              objc_setAssociatedObject(self, &ToastKeys.Queue, queue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
              return queue
             }
            }
           }
          
           // MARK: - Make Toast Methods
           func makeToast(message: String) {
            self.makeToast(message, duration: ToastManager.shared.duration, position: ToastManager.shared.position)
           }
           func makeToast(message: String, duration: NSTimeInterval, position: ToastPosition) {
            self.makeToast(message, duration: duration, position: position, style: nil)
           }
           func makeToast(message: String, duration: NSTimeInterval, position: CGPoint) {
            self.makeToast(message, duration: duration, position: position, style: nil)
           }
           func makeToast(message: String, duration: NSTimeInterval, position: ToastPosition, style: ToastStyle?) {
            self.makeToast(message, duration: duration, position: position, title: nil, image: nil, style: style, completion: nil)
           }
           func makeToast(message: String, duration: NSTimeInterval, position: CGPoint, style: ToastStyle?) {
            self.makeToast(message, duration: duration, position: position, title: nil, image: nil, style: style, completion: nil)
           }
           func makeToast(message: String?, duration: NSTimeInterval, position: ToastPosition, title: String?, image: UIImage?, style: ToastStyle?, completion: ((didTap: Bool) -> Void)?) {
            var toastStyle = ToastManager.shared.style
            if let style = style {
             toastStyle = style
            }
          
            do {
             let toast = try self.toastViewForMessage(message, title: title, image: image, style: toastStyle)
             self.showToast(toast, duration: duration, position: position, completion: completion)
            } catch ToastError.InsufficientData {
             print("Error: message, title, and image are all nil")
            } catch {}
           }
          
           func makeToast(message: String?, duration: NSTimeInterval, position: CGPoint, title: String?, image: UIImage?, style: ToastStyle?, completion: ((didTap: Bool) -> Void)?) {
            var toastStyle = ToastManager.shared.style
            if let style = style {
             toastStyle = style
            }
          
            do {
             let toast = try self.toastViewForMessage(message, title: title, image: image, style: toastStyle)
             self.showToast(toast, duration: duration, position: position, completion: completion)
            } catch ToastError.InsufficientData {
             print("Error: message, title, and image cannot all be nil")
            } catch {}
           }
          
           // MARK: - Show Toast Methods
           func showToast(toast: UIView) {
            self.showToast(toast, duration: ToastManager.shared.duration, position: ToastManager.shared.position, completion: nil)
           }
           func showToast(toast: UIView, duration: NSTimeInterval, position: ToastPosition, completion: ((didTap: Bool) -> Void)?) {
            let point = self.centerPointForPosition(position, toast: toast)
            self.showToast(toast, duration: duration, position: point, completion: completion)
           }
          
           func showToast(toast: UIView, duration: NSTimeInterval, position: CGPoint, completion: ((didTap: Bool) -> Void)?) {
            objc_setAssociatedObject(toast, &ToastKeys.Completion, ToastCompletionWrapper(completion), .OBJC_ASSOCIATION_RETAIN_NONATOMIC);
          
            if let _ = objc_getAssociatedObject(self, &ToastKeys.ActiveToast) as? UIView where ToastManager.shared.queueEnabled {
             objc_setAssociatedObject(toast, &ToastKeys.Duration, NSNumber(double: duration), .OBJC_ASSOCIATION_RETAIN_NONATOMIC);
             objc_setAssociatedObject(toast, &ToastKeys.Position, NSValue(CGPoint: position), .OBJC_ASSOCIATION_RETAIN_NONATOMIC);
          
             self.queue.addObject(toast)
            } else {
             self.showToast(toast, duration: duration, position: position)
            }
           }
          
           // MARK: - Activity Methods
           func makeToastActivity(position: ToastPosition) {
            // sanity
            if let _ = objc_getAssociatedObject(self, &ToastKeys.ActiveToast) as? UIView {
             return
            }
          
            let toast = self.createToastActivityView()
            let point = self.centerPointForPosition(position, toast: toast)
            self.makeToastActivity(toast, position: point)
           }
           func makeToastActivity(position: CGPoint) {
            // sanity
            if let _ = objc_getAssociatedObject(self, &ToastKeys.ActiveToast) as? UIView {
             return
            }
          
            let toast = self.createToastActivityView()
            self.makeToastActivity(toast, position: position)
           }
          
           //Dismisses the active toast activity indicator view.
           func hideToastActivity() {
            if let toast = objc_getAssociatedObject(self, &ToastKeys.ActivityView) as? UIView {
             UIView.animateWithDuration(ToastManager.shared.style.fadeDuration, delay: 0.0, options: [.CurveEaseIn, .BeginFromCurrentState], animations: { () -> Void in
              toast.alpha = 0.0
              }, completion: { (finished: Bool) -> Void in
               toast.removeFromSuperview()
               objc_setAssociatedObject(self, &ToastKeys.ActivityView, nil, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
             })
            }
           }
          
           // MARK: - Private Activity Methods
           private func makeToastActivity(toast: UIView, position: CGPoint) {
            toast.alpha = 0.0
            toast.center = position
          
            objc_setAssociatedObject(self, &ToastKeys.ActivityView, toast, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
          
            self.addSubview(toast)
          
            UIView.animateWithDuration(ToastManager.shared.style.fadeDuration, delay: 0.0, options: .CurveEaseOut, animations: { () -> Void in
             toast.alpha = 1.0
             }, completion: nil)
           }
          
           private func createToastActivityView() -> UIView {
            let style = ToastManager.shared.style
          
            let activityView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: style.activitySize.width, height: style.activitySize.height))
            activityView.backgroundColor = style.backgroundColor
            activityView.autoresizingMask = [.FlexibleLeftMargin, .FlexibleRightMargin, .FlexibleTopMargin, .FlexibleBottomMargin]
            activityView.layer.cornerRadius = style.cornerRadius
          
            if style.displayShadow {
             activityView.layer.shadowColor = style.shadowColor.CGColor
             activityView.layer.shadowOpacity = style.shadowOpacity
             activityView.layer.shadowRadius = style.shadowRadius
             activityView.layer.shadowOffset = style.shadowOffset
            }
          
            let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
            activityIndicatorView.center = CGPoint(x: activityView.bounds.size.width / 2.0, y: activityView.bounds.size.height / 2.0)
            activityView.addSubview(activityIndicatorView)
            activityIndicatorView.startAnimating()
          
            return activityView
           }
          
           // MARK: - Private Show/Hide Methods
           private func showToast(toast: UIView, duration: NSTimeInterval, position: CGPoint) {
            toast.center = position
            toast.alpha = 0.0
          
            if ToastManager.shared.tapToDismissEnabled {
             let recognizer = UITapGestureRecognizer(target: self, action: "handleToastTapped:")
             toast.addGestureRecognizer(recognizer)
             toast.userInteractionEnabled = true
             toast.exclusiveTouch = true
            }
          
            objc_setAssociatedObject(self, &ToastKeys.ActiveToast, toast, .OBJC_ASSOCIATION_RETAIN_NONATOMIC);
          
            self.addSubview(toast)
          
            UIView.animateWithDuration(ToastManager.shared.style.fadeDuration, delay: 0.0, options: [.CurveEaseOut, .AllowUserInteraction], animations: { () -> Void in
             toast.alpha = 1.0
             }) { (Bool finished) -> Void in
              let timer = NSTimer(timeInterval: duration, target: self, selector: "toastTimerDidFinish:", userInfo: toast, repeats: false)
              NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
              objc_setAssociatedObject(toast, &ToastKeys.Timer, timer, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
           }
          
           private func hideToast(toast: UIView) {
            self.hideToast(toast, fromTap: false)
           }
          
           private func hideToast(toast: UIView, fromTap: Bool) {
          
            UIView.animateWithDuration(ToastManager.shared.style.fadeDuration, delay: 0.0, options: [.CurveEaseIn, .BeginFromCurrentState], animations: { () -> Void in
             toast.alpha = 0.0
             }) { (didFinish: Bool) -> Void in
              toast.removeFromSuperview()
          
              objc_setAssociatedObject(self, &ToastKeys.ActiveToast, nil, .OBJC_ASSOCIATION_RETAIN_NONATOMIC);
          
              if let wrapper = objc_getAssociatedObject(toast, &ToastKeys.Completion) as? ToastCompletionWrapper, completion = wrapper.completion {
               completion(fromTap)
              }
          
              if let nextToast = self.queue.firstObject as? UIView, duration = objc_getAssociatedObject(nextToast, &ToastKeys.Duration) as? NSNumber, position = objc_getAssociatedObject(nextToast, &ToastKeys.Position) as? NSValue {
               self.queue.removeObjectAtIndex(0)
               self.showToast(nextToast, duration: duration.doubleValue, position: position.CGPointValue())
              }
            }
           }
          
           // MARK: - Events
           func handleToastTapped(recognizer: UITapGestureRecognizer) {
            if let toast = recognizer.view, timer = objc_getAssociatedObject(toast, &ToastKeys.Timer) as? NSTimer {
             timer.invalidate()
             self.hideToast(toast, fromTap: true)
            }
           }
          
           func toastTimerDidFinish(timer: NSTimer) {
            if let toast = timer.userInfo as? UIView {
             self.hideToast(toast)
            }
           }
          
           // MARK: - Toast Construction
           func toastViewForMessage(message: String?, title: String?, image: UIImage?, style: ToastStyle) throws -> UIView {
            // sanity
            if message == nil && title == nil && image == nil {
             throw ToastError.InsufficientData
            }
          
            var messageLabel: UILabel?
            var titleLabel: UILabel?
            var imageView: UIImageView?
          
            let wrapperView = UIView()
            wrapperView.backgroundColor = style.backgroundColor
            wrapperView.autoresizingMask = [.FlexibleLeftMargin, .FlexibleRightMargin, .FlexibleTopMargin, .FlexibleBottomMargin]
            wrapperView.layer.cornerRadius = style.cornerRadius
          
            if style.displayShadow {
             wrapperView.layer.shadowColor = UIColor.blackColor().CGColor
             wrapperView.layer.shadowOpacity = style.shadowOpacity
             wrapperView.layer.shadowRadius = style.shadowRadius
             wrapperView.layer.shadowOffset = style.shadowOffset
            }
          
            if let image = image {
             imageView = UIImageView(image: image)
             imageView?.contentMode = .ScaleAspectFit
             imageView?.frame = CGRect(x: style.horizontalPadding, y: style.verticalPadding, width: style.imageSize.width, height: style.imageSize.height)
            }
          
            var imageRect = CGRectZero
          
            if let imageView = imageView {
             imageRect.origin.x = style.horizontalPadding
             imageRect.origin.y = style.verticalPadding
             imageRect.size.width = imageView.bounds.size.width
             imageRect.size.height = imageView.bounds.size.height
            }
          
            if let title = title {
             titleLabel = UILabel()
             titleLabel?.numberOfLines = style.titleNumberOfLines
             titleLabel?.font = style.titleFont
             titleLabel?.textAlignment = style.titleAlignment
             titleLabel?.lineBreakMode = .ByTruncatingTail
             titleLabel?.textColor = style.titleColor
             titleLabel?.backgroundColor = UIColor.clearColor();
             titleLabel?.text = title;
          
             let maxTitleSize = CGSize(width: (self.bounds.size.width * style.maxWidthPercentage) - imageRect.size.width, height: self.bounds.size.height * style.maxHeightPercentage)
             let titleSize = titleLabel?.sizeThatFits(maxTitleSize)
             if let titleSize = titleSize {
              titleLabel?.frame = CGRect(x: 0.0, y: 0.0, width: titleSize.width, height: titleSize.height)
             }
            }
          
            if let message = message {
             messageLabel = UILabel()
             messageLabel?.text = message
             messageLabel?.numberOfLines = style.messageNumberOfLines
             messageLabel?.font = style.messageFont
             messageLabel?.textAlignment = style.messageAlignment
             messageLabel?.lineBreakMode = .ByTruncatingTail;
             messageLabel?.textColor = style.messageColor
             messageLabel?.backgroundColor = UIColor.clearColor()
          
             let maxMessageSize = CGSize(width: (self.bounds.size.width * style.maxWidthPercentage) - imageRect.size.width, height: self.bounds.size.height * style.maxHeightPercentage)
             let messageSize = messageLabel?.sizeThatFits(maxMessageSize)
             if let messageSize = messageSize {
              messageLabel?.frame = CGRect(x: 0.0, y: 0.0, width: messageSize.width, height: messageSize.height)
             }
            }
          
            var titleRect = CGRectZero
          
            if let titleLabel = titleLabel {
             titleRect.origin.x = imageRect.origin.x + imageRect.size.width + style.horizontalPadding
             titleRect.origin.y = style.verticalPadding
             titleRect.size.width = titleLabel.bounds.size.width
             titleRect.size.height = titleLabel.bounds.size.height
            }
          
            var messageRect = CGRectZero
          
            if let messageLabel = messageLabel {
             messageRect.origin.x = imageRect.origin.x + imageRect.size.width + style.horizontalPadding
             messageRect.origin.y = titleRect.origin.y + titleRect.size.height + style.verticalPadding
             messageRect.size.width = messageLabel.bounds.size.width
             messageRect.size.height = messageLabel.bounds.size.height
            }
          
            let longerWidth = max(titleRect.size.width, messageRect.size.width)
            let longerX = max(titleRect.origin.x, messageRect.origin.x)
            let wrapperWidth = max((imageRect.size.width + (style.horizontalPadding * 2.0)), (longerX + longerWidth + style.horizontalPadding))
            let wrapperHeight = max((messageRect.origin.y + messageRect.size.height + style.verticalPadding), (imageRect.size.height + (style.verticalPadding * 2.0)))
          
            wrapperView.frame = CGRect(x: 0.0, y: 0.0, width: wrapperWidth, height: wrapperHeight)
          
            if let titleLabel = titleLabel {
             titleLabel.frame = titleRect
             wrapperView.addSubview(titleLabel)
            }
          
            if let messageLabel = messageLabel {
             messageLabel.frame = messageRect
             wrapperView.addSubview(messageLabel)
            }
          
            if let imageView = imageView {
             wrapperView.addSubview(imageView)
            }
          
            return wrapperView
           }
          
           // MARK: - Helpers
           private func centerPointForPosition(position: ToastPosition, toast: UIView) -> CGPoint {
            let padding: CGFloat = ToastManager.shared.style.verticalPadding
          
            switch(position) {
            case .Top:
             return CGPoint(x: self.bounds.size.width / 2.0, y: (toast.frame.size.height / 2.0) + padding)
            case .Center:
             return CGPoint(x: self.bounds.size.width / 2.0, y: self.bounds.size.height / 2.0)
            case .Bottom:
             return CGPoint(x: self.bounds.size.width / 2.0, y: (self.bounds.size.height - (toast.frame.size.height / 2.0)) - padding)
            }
           }
          }
          
          // MARK: - Toast Style
          struct ToastStyle {
          
           var backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.8)
           var titleColor = UIColor.whiteColor()
           var messageColor = UIColor.whiteColor()
           var maxWidthPercentage: CGFloat = 0.8 {
            didSet {
             maxWidthPercentage = max(min(maxWidthPercentage, 1.0), 0.0)
            }
           }
          
          
           var maxHeightPercentage: CGFloat = 0.8 {
            didSet {
             maxHeightPercentage = max(min(maxHeightPercentage, 1.0), 0.0)
            }
           }
          
           var horizontalPadding: CGFloat = 10.0
           var verticalPadding: CGFloat = 10.0
           var cornerRadius: CGFloat = 10.0;
           var titleFont = UIFont.boldSystemFontOfSize(16.0)
           var messageFont = UIFont.systemFontOfSize(16.0)
           var titleAlignment = NSTextAlignment.Left
           var messageAlignment = NSTextAlignment.Left
           var titleNumberOfLines = 0;
           var messageNumberOfLines = 0;
           var displayShadow = false;
           var shadowColor = UIColor.blackColor()
           var shadowOpacity: Float = 0.8 {
            didSet {
             shadowOpacity = max(min(shadowOpacity, 1.0), 0.0)
            }
           }
          
           var shadowRadius: CGFloat = 6.0
           var shadowOffset = CGSize(width: 4.0, height: 4.0)
           var imageSize = CGSize(width: 80.0, height: 80.0)
           var activitySize = CGSize(width: 100.0, height: 100.0)
           var fadeDuration: NSTimeInterval = 0.2
          
          }
          
          // MARK: - Toast Manager
          class ToastManager {
          
           static let shared = ToastManager()
           var style = ToastStyle()
           var tapToDismissEnabled = true
           var queueEnabled = true
           var duration: NSTimeInterval = 3.0
           var position = ToastPosition.Bottom
          
          }
          

          使用 Toast.swift:

          // basic usage
            self.view.makeToast("Sample Toast")
          
            // toast with a specific duration and position
            self.view.makeToast("Sample Toast", duration: 3.0, position: .Top)
          
            // toast with all possible options
            self.view.makeToast("Sample Toast", duration: 2.0, position: CGPoint(x: 110.0, y: 110.0), title: "Toast Title", image: UIImage(named: "ic_120x120.png"), style:nil) { (didTap: Bool) -> Void in
             if didTap {
              print("completion from tap")
             } else {
              print("completion without tap")
             }
            }
          
            //display toast with an activity spinner
             self.view.makeToastActivity(.Center)
          
            // display any view as toast
            let sampleView = UIView(frame: CGRectMake(100,100,200,200))
            sampleView.backgroundColor = UIColor(patternImage: UIImage(named: "ic_120x120")!)
          
            self.view.showToast(sampleView)
            self.view.showToast(sampleView, duration: 3.0, position: .Top, completion: nil)
          

          您可以从https://github.com/alvinreuben/ToastSample下载示例项目

          【讨论】:

            【解决方案11】:

            这是你需要的东西。使用大量动画选项、屏幕位置和持续时间创建。即使您可以提供自己的持续时间。请在下面查看。

            https://github.com/avistyles/AVIToast

            【讨论】:

              【解决方案12】:

              斯威夫特 3

              已经非常高兴地使用Rannie/Toast-Swift for Swift 3 有一段时间了,可以推荐它以获得非常相似的“类似 Android”的体验。它的实现非常简单,无需另一个 pod,并且可以根据您的需要进行自定义。

              简单易学

              view.makeToastActivity()
              view.hideToastActivity()
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2011-10-12
                • 1970-01-01
                • 2013-07-17
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-01-01
                相关资源
                最近更新 更多