No Description

ToastView.swift 6.1KB

    // // ToastView.swift // PaiAi // // Created by mac on 16/7/21. // Copyright © 2016年 FFIB. All rights reserved. // import UIKit class ToastView: UIView { fileprivate var option: ToastOption fileprivate var toastType: Toast.ToastType fileprivate var label: UILabel? fileprivate var imageView: UIImageView? fileprivate var activityIndicatorView: UIActivityIndicatorView? fileprivate var text: String? fileprivate var image: UIImage? fileprivate var isActivity: Bool = false fileprivate var topView: UIView? fileprivate var bottomView: UIView? fileprivate var padding = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12) init(option: ToastOption, type: Toast.ToastType) { self.option = option self.toastType = type super.init(frame: CGRect.zero) initProperty() } @available(*, unavailable, message: "Loading this view from a nib is unsupported") public required init?(coder aDecoder: NSCoder) { fatalError("Loading this view from a nib is unsupported") } private func initProperty() { alpha = 0 layer.cornerRadius = 5 backgroundColor = option.backgroundColor } override func didMoveToWindow() { switch toastType { case let .text(t): text = t case let .image(i): image = i case let .imageWithText(i, t): image = i text = t case let .activityIndicator(t): text = t isActivity = true } constructViewHierarchy() activateConstraints() } } /// construct view and layout fileprivate extension ToastView { func constructViewHierarchy() { if image != nil { imageView = makeImageView() addSubview(imageView!) } if isActivity { activityIndicatorView = makeActivityIndicatorView() addSubview(activityIndicatorView!) } if text != nil { label = makeLabel() addSubview(label!) } } func activateConstraints() { activateConstraintsImage() activateConstraintsIndicator() activateConstraintsText() activateConstraintsContentView() } func activateConstraintsImage() { guard let imageView = imageView else { return } imageView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ imageView.widthAnchor.constraint(equalToConstant: 50), imageView.heightAnchor.constraint(equalToConstant: 50), imageView.centerXAnchor.constraint(equalTo: centerXAnchor), imageView.topAnchor.constraint(equalTo: topAnchor, constant: padding.top) ]) topView = imageView } func activateConstraintsIndicator() { guard let activityIndicatorView = activityIndicatorView else { return } activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ activityIndicatorView.centerXAnchor.constraint(equalTo: centerXAnchor), activityIndicatorView.topAnchor.constraint(equalTo: topAnchor, constant: padding.top) ]) topView = activityIndicatorView activityIndicatorView.startAnimating() } func activateConstraintsText() { guard let label = label else { return } let windowSize = UIScreen.main.bounds.size label.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ label.centerXAnchor.constraint(equalTo: centerXAnchor), widthAnchor.constraint(lessThanOrEqualToConstant: windowSize.width - 52), label.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -padding.bottom), label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: padding.left), label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -padding.right), label.topAnchor.constraint(equalTo: topView?.bottomAnchor ?? topAnchor, constant: 6), ]) if (text?.count ?? 0) <= 3 { } else { NSLayoutConstraint.activate([ ]) } if topView != nil { NSLayoutConstraint.activate([ label.widthAnchor.constraint(greaterThanOrEqualTo: heightAnchor) ]) } bottomView = label } func activateConstraintsContentView() { guard let v = superview else { return } translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ centerXAnchor.constraint(equalTo: v.centerXAnchor), centerYAnchor.constraint(equalTo: v.centerYAnchor) ]) if topView == nil { NSLayoutConstraint.activate([ bottomView!.topAnchor.constraint(equalTo: topAnchor, constant: padding.top) ]) } else if bottomView == nil { NSLayoutConstraint.activate([ topView!.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -padding.bottom), topView!.leadingAnchor.constraint(equalTo: leadingAnchor, constant: padding.left), topView!.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -padding.right) ]) } } } fileprivate extension ToastView { func makeLabel() -> UILabel { let l = UILabel() l.text = text l.numberOfLines = 0 l.font = option.font l.textAlignment = .center l.textColor = option.tintColor return l } func makeImageView() -> UIImageView { let i = UIImageView() i.image = image return i } func makeActivityIndicatorView() -> UIActivityIndicatorView { let a = UIActivityIndicatorView(style: .white) return a } }