|
@@ -1,260 +0,0 @@
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-import UIKit
|
|
|
-
|
|
|
-private var kBadgeView = "kNXBadgeView"
|
|
|
-
|
|
|
-
|
|
|
-public extension NX where Base: UIView {
|
|
|
-
|
|
|
- var badgeView: NXBadgeControl {
|
|
|
- return base.badgeView
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- func addBadge(text: String?) {
|
|
|
- showBadge()
|
|
|
- base.badgeView.text = text
|
|
|
- setBadge(flexMode: base.badgeView.flexMode)
|
|
|
- if text == nil {
|
|
|
- if base.badgeView.widthConstraint()?.relation == .equal { return }
|
|
|
- base.badgeView.widthConstraint()?.isActive = false
|
|
|
- let constraint = NSLayoutConstraint(item: base.badgeView, attribute: .width, relatedBy: .equal, toItem: base.badgeView, attribute: .height, multiplier: 1.0, constant: 0)
|
|
|
- base.badgeView.addConstraint(constraint)
|
|
|
- } else {
|
|
|
- if base.badgeView.widthConstraint()?.relation == .greaterThanOrEqual { return }
|
|
|
- base.badgeView.widthConstraint()?.isActive = false
|
|
|
- let constraint = NSLayoutConstraint(item: base.badgeView, attribute: .width, relatedBy: .greaterThanOrEqual, toItem: base.badgeView, attribute: .height, multiplier: 1.0, constant: 0)
|
|
|
- base.badgeView.addConstraint(constraint)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- func addBadge(number: Int) {
|
|
|
- if number <= 0 {
|
|
|
- addBadge(text: "0")
|
|
|
- hiddenBadge()
|
|
|
- return
|
|
|
- }
|
|
|
- addBadge(text: "\(number)")
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- func addDot(color: UIColor? = .red) {
|
|
|
- addBadge(text: nil)
|
|
|
- setBadge(height: 8.0)
|
|
|
- base.badgeView.backgroundColor = color
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- func moveBadge(x: CGFloat, y: CGFloat) {
|
|
|
- base.badgeView.offset = CGPoint(x: x, y: y)
|
|
|
- base.centerYConstraint(with: base.badgeView)?.constant = y
|
|
|
-
|
|
|
- let badgeHeight = base.badgeView.heightConstraint()?.constant ?? 0
|
|
|
- switch base.badgeView.flexMode {
|
|
|
- case .head:
|
|
|
- base.centerXConstraint(with: base.badgeView)?.isActive = false
|
|
|
- base.leadingConstraint(with: base.badgeView)?.isActive = false
|
|
|
- if let constraint = base.trailingConstraint(with: base.badgeView) {
|
|
|
- constraint.constant = badgeHeight * 0.5 + x
|
|
|
- return
|
|
|
- }
|
|
|
- let trailingConstraint = NSLayoutConstraint(item: base.badgeView, attribute: .trailing, relatedBy: .equal, toItem: base, attribute: .trailing, multiplier: 1.0, constant: badgeHeight * 0.5 + x)
|
|
|
- base.addConstraint(trailingConstraint)
|
|
|
-
|
|
|
- case .tail:
|
|
|
- base.centerXConstraint(with: base.badgeView)?.isActive = false
|
|
|
- base.trailingConstraint(with: base.badgeView)?.isActive = false
|
|
|
- if let constraint = base.leadingConstraint(with: base.badgeView) {
|
|
|
- constraint.constant = x - badgeHeight * 0.5
|
|
|
- return
|
|
|
- }
|
|
|
- let leadingConstraint = NSLayoutConstraint(item: base.badgeView, attribute: .leading, relatedBy: .equal, toItem: base, attribute: .trailing, multiplier: 1.0, constant: x - badgeHeight * 0.5)
|
|
|
- base.addConstraint(leadingConstraint)
|
|
|
-
|
|
|
- case .middle:
|
|
|
- base.leadingConstraint(with: base.badgeView)?.isActive = false
|
|
|
- base.trailingConstraint(with: base.badgeView)?.isActive = false
|
|
|
- base.centerXConstraint(with: base.badgeView)?.constant = x
|
|
|
- if let constraint = base.centerXConstraint(with: base.badgeView) {
|
|
|
- constraint.constant = x
|
|
|
- return
|
|
|
- }
|
|
|
- let centerXConstraint = NSLayoutConstraint(item: base.badgeView, attribute: .centerX, relatedBy: .equal, toItem: base, attribute: .centerX, multiplier: 1.0, constant: x)
|
|
|
- base.addConstraint(centerXConstraint)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- func setBadge(flexMode: NXBadgeViewFlexMode = .tail) {
|
|
|
- base.badgeView.flexMode = flexMode
|
|
|
- moveBadge(x: base.badgeView.offset.x, y: base.badgeView.offset.y)
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- func setBadge(height: CGFloat) {
|
|
|
- base.badgeView.layer.cornerRadius = height * 0.5
|
|
|
- base.badgeView.heightConstraint()?.constant = height
|
|
|
- moveBadge(x: base.badgeView.offset.x, y: base.badgeView.offset.y)
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- func showBadge() {
|
|
|
- base.badgeView.isHidden = false
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- func hiddenBadge() {
|
|
|
- base.badgeView.isHidden = true
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- func increase() {
|
|
|
- increaseBy(number: 1)
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- func increaseBy(number: Int) {
|
|
|
- let label = base.badgeView
|
|
|
- let result = (Int(label.text ?? "0") ?? 0) + number
|
|
|
- if result > 0 {
|
|
|
- showBadge()
|
|
|
- }
|
|
|
- label.text = "\(result)"
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- func decrease() {
|
|
|
- decreaseBy(number: 1)
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- func decreaseBy(number: Int) {
|
|
|
- let label = base.badgeView
|
|
|
- let result = (Int(label.text ?? "0") ?? 0) - number
|
|
|
- if (result <= 0) {
|
|
|
- hiddenBadge()
|
|
|
- label.text = "0"
|
|
|
- return
|
|
|
- }
|
|
|
- label.text = "\(result)"
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-extension UIView {
|
|
|
-
|
|
|
- public func addBadgeViewLayoutConstraint() {
|
|
|
- badgeView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
- let centerXConstraint = NSLayoutConstraint(item: badgeView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: 0)
|
|
|
- let centerYConstraint = NSLayoutConstraint(item: badgeView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0)
|
|
|
- let widthConstraint = NSLayoutConstraint(item: badgeView, attribute: .width, relatedBy: .greaterThanOrEqual, toItem: badgeView, attribute: .height, multiplier: 1.0, constant: 0)
|
|
|
- let heightConstraint = NSLayoutConstraint(item: badgeView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 18)
|
|
|
- addConstraints([centerXConstraint, centerYConstraint])
|
|
|
- badgeView.addConstraints([widthConstraint, heightConstraint])
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-extension UIView {
|
|
|
-
|
|
|
- public var badgeView: NXBadgeControl {
|
|
|
- get {
|
|
|
- if let aValue = objc_getAssociatedObject(self, &kBadgeView) as? NXBadgeControl {
|
|
|
- return aValue
|
|
|
- }
|
|
|
- else {
|
|
|
- let badgeControl = NXBadgeControl.default()
|
|
|
- self.addSubview(badgeControl)
|
|
|
- self.bringSubviewToFront(badgeControl)
|
|
|
- self.badgeView = badgeControl
|
|
|
- self.addBadgeViewLayoutConstraint()
|
|
|
- return badgeControl
|
|
|
- }
|
|
|
- }
|
|
|
- set {
|
|
|
- objc_setAssociatedObject(self, &kBadgeView, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public func topConstraint(with item: AnyObject?) -> NSLayoutConstraint? {
|
|
|
- return constraint(with: item, attribute: .top)
|
|
|
- }
|
|
|
-
|
|
|
- public func leadingConstraint(with item: AnyObject?) -> NSLayoutConstraint? {
|
|
|
- return constraint(with: item, attribute: .leading)
|
|
|
- }
|
|
|
-
|
|
|
- public func bottomConstraint(with item: AnyObject?) -> NSLayoutConstraint? {
|
|
|
- return constraint(with: item, attribute: .bottom)
|
|
|
- }
|
|
|
-
|
|
|
- public func trailingConstraint(with item: AnyObject?) -> NSLayoutConstraint? {
|
|
|
- return constraint(with: item, attribute: .trailing)
|
|
|
- }
|
|
|
-
|
|
|
- public func widthConstraint() -> NSLayoutConstraint? {
|
|
|
- return constraint(with: self, attribute: .width)
|
|
|
- }
|
|
|
-
|
|
|
- public func heightConstraint() -> NSLayoutConstraint? {
|
|
|
- return constraint(with: self, attribute: .height)
|
|
|
- }
|
|
|
-
|
|
|
- public func centerXConstraint(with item: AnyObject?) -> NSLayoutConstraint? {
|
|
|
- return constraint(with: item, attribute: .centerX)
|
|
|
- }
|
|
|
-
|
|
|
- public func centerYConstraint(with item: AnyObject?) -> NSLayoutConstraint? {
|
|
|
- return constraint(with: item, attribute: .centerY)
|
|
|
- }
|
|
|
-
|
|
|
- public func constraint(with item: AnyObject?, attribute: NSLayoutConstraint.Attribute) -> NSLayoutConstraint? {
|
|
|
- for constraint in constraints {
|
|
|
- if let isSame = constraint.firstItem?.isEqual(item), isSame, constraint.firstAttribute == attribute {
|
|
|
- return constraint
|
|
|
- }
|
|
|
- }
|
|
|
- return nil
|
|
|
- }
|
|
|
-}
|