No Description

FFToastView.swift 9.3KB

    // // FFToastView.swift // PaiAi // // Created by mac on 16/7/21. // Copyright © 2016年 FFIB. All rights reserved. // import UIKit class FFToastView: UIView { //文字 var text: String = "" { didSet { label.text = text label.numberOfLines = 0 label.lineBreakMode = .byWordWrapping label.textAlignment = .center label.sizeToFit() } } //字体 var textFont = UIFont.systemFont(ofSize: 14) var textColor = UIColor.white //转菊花等待指示 lazy var activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge) // 与父view的centerX距离 var centerXPadding: CGFloat = 0 // 与父view的centerX比例 var centerXMultiplier: CGFloat = 1 // -- var centerYPadding: CGFloat = 0 var centerYMultiplier: CGFloat = 1 // 中间文字上下左右间距 var labelEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) { didSet { if !UIEdgeInsetsEqualToEdgeInsets(labelEdgeInsets, oldValue) { self.setNeedsUpdateConstraints() } } } // 边界圆角 var viewCornerRadius: CGFloat = 5 { didSet { self.layer.masksToBounds = true self.layer.cornerRadius = viewCornerRadius } } fileprivate var showing = false //文字toast func showToast(inView superview: UIView, duration: TimeInterval) { showing = true self.superview?.isUserInteractionEnabled = true // if label.superview == self { label.removeFromSuperview() // } self.addToSuperView(superview, duration: duration, haveImage: true) self.addSubview(label) activityIndicatorView.removeFromSuperview() addLabelConstraints() } func showImageToast(inView superview: UIView, duration: TimeInterval) { showing = true self.superview?.isUserInteractionEnabled = true label.removeFromSuperview() imageView.removeFromSuperview() self.addToSuperView(superview, duration: duration) // if label.superview != self { self.addSubview(label) self.addSubview(imageView) activityIndicatorView.removeFromSuperview() addImageViewConstraints() // } } //菊花toast func showLoadingToast(inView superview: UIView, blockSuperView: Bool) { showing = true self.superview?.isUserInteractionEnabled = true label.removeFromSuperview() self.addAcitvityIndicatorAndAddConstraints() activityIndicatorView.startAnimating() if blockSuperView { superview.isUserInteractionEnabled = false } self.addToSuperView(superview, duration: 0) } func hideLoadingToast() { showing = false activityIndicatorView.stopAnimating() activityIndicatorView.removeFromSuperview() self.dismiss() } func addToSuperView(_ superview: UIView, duration: TimeInterval, haveImage: Bool = false) { self.removeFromSuperview() superview.addSubview(self) self.layer.removeAllAnimations() self.alpha = 1 // constrains to superview self.useAutoLayout() self.constraintCenterX = NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: superview, attribute: .centerX, multiplier: centerXMultiplier, constant: centerXPadding) self.constraintCenterX!.active() self.constraintCenterY = NSLayoutConstraint(item: self, attribute: .centerY, relatedBy: .equal, toItem: superview, attribute: .centerY, multiplier: centerYMultiplier, constant: centerYPadding) self.constraintCenterY!.active() if self.bounds.width == 0 { self.superview?.layoutIfNeeded() } self.layoutIfNeeded() if duration > 0 { self.perform(#selector(FFToastView.dismiss), with: nil, afterDelay: duration) } } @objc func dismiss() { showing = false UIView.animate(withDuration: 0.2, animations: { self.alpha = 0 }, completion: { (_) in self.superview?.isUserInteractionEnabled = true if !self.showing { self.removeFromSuperview() } self.alpha = 1 }) } @discardableResult class func showToast(inView superview: UIView, withText text: String) -> FFToastView { defaultInstance.text = text defaultInstance.showToast(inView: superview, duration: 2) return defaultInstance } @discardableResult class func showToastAndTime(inView superview: UIView, withText text: String, withDuration duration: TimeInterval) -> FFToastView { defaultInstance.text = text defaultInstance.showToast(inView: superview, duration: duration) return defaultInstance } @discardableResult class func showImageToast(inView superview: UIView, withText text: String, withImage image: String) -> FFToastView { defaultInstance.text = text defaultInstance.imageView.image = UIImage(named: image) defaultInstance.showImageToast(inView: superview, duration: 2) return defaultInstance } class func hideLoadingToast() { defaultInstance.hideLoadingToast() } @discardableResult class func showLoadingToast(inView superview: UIView, blockSuperView block: Bool) -> FFToastView { defaultInstance.showLoadingToast(inView: superview, blockSuperView : block) return defaultInstance } // MARK: - -private-- static fileprivate let defaultInstance = FFToastView(frame: CGRect.zero) fileprivate let label = UILabel() fileprivate let imageView = UIImageView() fileprivate var constraintCenterX: NSLayoutConstraint? fileprivate var constraintCenterY: NSLayoutConstraint? fileprivate var innerConstraints: [NSLayoutConstraint] = [] fileprivate func applyDefaultSetting() { self.backgroundColor = UIColor(white: 0, alpha: 0.8) label.textColor = self.textColor label.font = self.textFont self.viewCornerRadius = 5 } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.applyDefaultSetting() } override init(frame: CGRect) { super.init(frame: frame) self.applyDefaultSetting() } override func didMoveToSuperview() { if superview == nil { self.constraintCenterX = nil self.constraintCenterY = nil } } func addImageViewConstraints() { self.removeConstraints(constraints) if imageView.superview == self && label.superview == self { let rect = NSString(string: text).boundingRect(with: CGSize(width: kScreenWidth - 50, height: 10000), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15)], context: nil) let size = NSString(string: text).size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)]) let width = size.width < kScreenWidth - 50 ? size.width : kScreenWidth - 50 imageView.frame = CGRect(x: 34, y: 18, width: 60, height: 60) label.frame = CGRect(x: 10, y: 90, width: width, height: rect.height) innerConstraints.removeAll(keepingCapacity: true) NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: width + 20).active() NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 114 + rect.height).active() } } func addLabelConstraints() { imageView.removeFromSuperview() self.removeConstraints(constraints) let rect = NSString(string: text).boundingRect(with: CGSize(width: kScreenWidth - 50, height: 10000), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15)], context: nil) let size = NSString(string: text).size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)]) let width = size.width < kScreenWidth - 50 ? size.width : kScreenWidth - 50 label.frame = CGRect(x: 10, y: 10, width: width, height: rect.height) NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: width + 20).active() NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: rect.height + 20).active() } fileprivate func addAcitvityIndicatorAndAddConstraints() { imageView.removeFromSuperview() removeConstraints(constraints) if activityIndicatorView.superview != self { self.addSubview(activityIndicatorView) activityIndicatorView.useAutoLayout() innerConstraints.removeAll() NSLayoutConstraint.constraints(withVisualFormat: "H:|-30-[Indicator]-30-|", options: [], metrics: nil, views: ["Indicator": activityIndicatorView]).autolayoutInstall() NSLayoutConstraint.constraints(withVisualFormat: "V:|-30-[Indicator]-30-|", options: [], metrics: nil, views: ["Indicator": activityIndicatorView]).autolayoutInstall() } } }