美文网首页Swift工作笔记
Swift.超简易Toast.100行代码,拖入即用.

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

作者: 王四猫 | 来源:发表于2018-09-25 09:19 被阅读27次
    效果图

    实现功能:

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

    支持自定义弹出位置以及持续时间.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)
    
    

    demo地址:EWToast.求star.

    有问题欢迎探讨.

    相关文章

      网友评论

        本文标题:Swift.超简易Toast.100行代码,拖入即用.

        本文链接:https://www.haomeiwen.com/subject/ivzmgftx.html