• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift.超简易Toast.100行代码,拖入即用.

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

实现功能:

一百行代码实现,拖入一个文件即可使用,轻便易用.

支持自定义弹出位置以及持续时间.size自适应弹出text文字长度.

实现点击toast消失功能.

实现对手机的旋转监听,以保证良好的显示体验.

实现方式:

1.新建一个类基于NSObject,为其添加显示View.

2.添加easyIn,easyOut动画.

3.添加show,dismiss方法.

4.用类方法的形式来实现使用toast的入口方法.

5.使用方法.


1.新建一个类基于NSObject,为其添加显示View.

    var _contentView: UIButton
    var _duration: CGFloat = ToastDispalyDuration

    init(text: String) {
        let rect = text.boundingRect(with: CGSize(width: 250, height: CGFloat.greatestFiniteMagnitude), options:[NSStringDrawingOptions.truncatesLastVisibleLine, NSStringDrawingOptions.usesFontLeading,NSStringDrawingOptions.usesLineFragmentOrigin], attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)], context: nil)
        let textLabel = UILabel(frame: CGRect(x: 0, y: 0, width: rect.size.width + 40, height: rect.size.height + 20))
        textLabel.backgroundColor = UIColor.clear
        textLabel.textColor = UIColor.white
        textLabel.textAlignment = .center
        textLabel.font = UIFont.systemFont(ofSize: 16)
        textLabel.text = text
        textLabel.numberOfLines = 0

        _contentView = UIButton(frame: CGRect(x: 0, y: 0, width: textLabel.frame.size.width, height: textLabel.frame.size.height))
        _contentView.layer.cornerRadius = 2.0
        _contentView.backgroundColor = ToastBackgroundColor
        _contentView.addSubview(textLabel)
        _contentView.autoresizingMask = UIViewAutoresizing.flexibleWidth
        super.init()
        _contentView.addTarget(self, action: #selector(toastTaped), for: .touchDown)
        ///添加通知获取手机旋转状态.保证正确的显示效果
        NotificationCenter.default.addObserver(self, selector: #selector(toastTaped), name: NSNotification.Name.UIDeviceOrientationDidChange, object: UIDevice.current)
    }

2.添加easyIn,easyOut动画.

     func showAnimation(){
        UIView.beginAnimations("show", context: nil)
        UIView.setAnimationCurve(UIViewAnimationCurve.easeIn)
        UIView.setAnimationDuration(0.3)
        _contentView.alpha = 1.0
        UIView.commitAnimations()
    }
    @objc func hideAnimation(){
        UIView.beginAnimations("hide", context: nil)
        UIView.setAnimationCurve(UIViewAnimationCurve.easeOut)
        UIView.setAnimationDelegate(self)
        UIView.setAnimationDidStop(#selector(dismissToast))
        UIView.setAnimationDuration(0.3)
        _contentView.alpha = 0.0
        UIView.commitAnimations()
    }

3.添加show,dismiss方法.

    func show(){
        let window: UIWindow = UIApplication.shared.windows.last!
        _contentView.center = window.center
        window.addSubview(_contentView)
        self.showAnimation()
        self.perform(#selector(hideAnimation), with: nil, afterDelay: TimeInterval(_duration))
    }
    func showFromTopOffset(top: CGFloat){
        let window: UIWindow = UIApplication.shared.windows.last!
        _contentView.center = CGPoint(x: window.center.x, y: top + _contentView.frame.size.height/2)
        window.addSubview(_contentView)
        self.showAnimation()
        self.perform(#selector(hideAnimation), with: nil, afterDelay: TimeInterval(_duration))
    }
    func showFromBottomOffset(bottom: CGFloat){
        let window: UIWindow = UIApplication.shared.windows.last!
        _contentView.center = CGPoint(x: window.center.x, y: window.frame.size.height - (bottom + _contentView.frame.size.height/2))
        window.addSubview(_contentView)
        self.showAnimation()
        self.perform(#selector(hideAnimation), with: nil, afterDelay: TimeInterval(_duration))
    }

    @objc func dismissToast(){
        _contentView.removeFromSuperview()
    }

4.用类方法的形式来实现使用toast的入口方法.

 //MARK: 中间显示
    class func showCenterWithText(text: String){
        EWToast.showCenterWithText(text: text, duration: CGFloat(ToastDispalyDuration))
    }
    class func showCenterWithText(text: String, duration: CGFloat){
        let toast: EWToast = EWToast(text: text)
        toast.setDuration(duration: duration)
        toast.show()
    }
    //MARK: 上方显示
    class func showTopWithText(text: String){
        EWToast.showTopWithText(text: text, topOffset: 100.0, duration: ToastDispalyDuration)
    }
    class func showTopWithText(text: String, duration: CGFloat){
        EWToast.showTopWithText(text: text, topOffset: 100, duration: duration)
    }
    class func showTopWithText(text: String, topOffset: CGFloat){
        EWToast.showTopWithText(text: text, topOffset: topOffset, duration: ToastDispalyDuration)
    }
    class func showTopWithText(text: String, topOffset: CGFloat, duration: CGFloat){
        let toast = EWToast(text: text)
        toast.setDuration(duration: duration)
        toast.showFromTopOffset(top: topOffset)
    }
    //MARK: 下方显示
    class func showBottomWithText(text: String){
        EWToast.showBottomWithText(text: text, bottomOffset: 100.0, duration: ToastDispalyDuration)
    }
    class func showBottomWithText(text: String, duration: CGFloat){
        EWToast.showBottomWithText(text: text, bottomOffset: 100.0, duration: duration)
    }
    class func showBottomWithText(text: String, bottomOffset: CGFloat){
        EWToast.showBottomWithText(text: text, bottomOffset: bottomOffset, duration: ToastDispalyDuration)
    }
    class func showBottomWithText(text: String, bottomOffset: CGFloat, duration: CGFloat){
        let toast = EWToast(text: text)
        toast.setDuration(duration: duration)
        toast.showFromBottomOffset(bottom: bottomOffset)
    }

#5.使用方法:

    EWToast.showTopWithText(text: "上方显示,持续默认时间2秒")

    EWToast.showCenterWithText(text: "中间显示,持续自定义时间3秒", duration: 3)

    EWToast.showBottomWithText(text: "下方显示,位置自定义距离屏幕底边150", bottomOffset: 150)


github地址:EWToast.

有问题欢迎探讨.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode899.有序队列|OrderlyQueue发布时间:2022-07-13
下一篇:
6.4 问号?和感叹号!的用法解析 [Swift原创教程]发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap