UITabBarItem+PPBadgeView.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // UITabBarItem+PPBadgeView.swift
  3. // PPBadgeViewSwift
  4. //
  5. // Created by AndyPang on 2017/6/19.
  6. // Copyright © 2017年 AndyPang. All rights reserved.
  7. //
  8. /*
  9. *********************************************************************************
  10. *
  11. * Weibo : jkpang-庞 ( http://weibo.com/jkpang )
  12. * Email : jkpang@outlook.com
  13. * QQ 群 : 323408051
  14. * GitHub: https://github.com/jkpang
  15. *
  16. *********************************************************************************
  17. */
  18. import UIKit
  19. public extension PP where Base: UITabBarItem {
  20. var badgeView: PPBadgeControl {
  21. return _bottomView.pp.badgeView
  22. }
  23. /// 添加带文本内容的Badge, 默认右上角, 红色, 18pts
  24. ///
  25. /// Add Badge with text content, the default upper right corner, red backgroundColor, 18pts
  26. ///
  27. /// - Parameter text: 文本字符串
  28. func addBadge(text: String) {
  29. _bottomView.pp.addBadge(text: text)
  30. _bottomView.pp.moveBadge(x: 4, y: 3)
  31. }
  32. /// 添加带数字的Badge, 默认右上角,红色,18pts
  33. ///
  34. /// Add the Badge with numbers, the default upper right corner, red backgroundColor, 18pts
  35. ///
  36. /// - Parameter number: 整形数字
  37. func addBadge(number: Int) {
  38. _bottomView.pp.addBadge(number: number)
  39. _bottomView.pp.moveBadge(x: 4, y: 3)
  40. }
  41. /// 添加带颜色的小圆点, 默认右上角, 红色, 8pts
  42. ///
  43. /// Add small dots with color, the default upper right corner, red backgroundColor, 8pts
  44. ///
  45. /// - Parameter color: 颜色
  46. func addDot(color: UIColor?) {
  47. _bottomView.pp.addDot(color: color)
  48. }
  49. /// 设置Badge的偏移量, Badge中心点默认为其父视图的右上角
  50. ///
  51. /// Set Badge offset, Badge center point defaults to the top right corner of its parent view
  52. ///
  53. /// - Parameters:
  54. /// - x: X轴偏移量 (x<0: 左移, x>0: 右移) axis offset (x <0: left, x> 0: right)
  55. /// - y: Y轴偏移量 (y<0: 上移, y>0: 下移) axis offset (Y <0: up, y> 0: down)
  56. func moveBadge(x: CGFloat, y: CGFloat) {
  57. _bottomView.pp.moveBadge(x: x, y: y)
  58. }
  59. /// 设置Badge伸缩的方向
  60. ///
  61. /// Setting the direction of Badge expansion
  62. ///
  63. /// PPBadgeViewFlexModeHead, 左伸缩 Head Flex : <==●
  64. /// PPBadgeViewFlexModeTail, 右伸缩 Tail Flex : ●==>
  65. /// PPBadgeViewFlexModeMiddle 左右伸缩 Middle Flex : <=●=>
  66. /// - Parameter flexMode : Default is PPBadgeViewFlexModeTail
  67. func setBadge(flexMode: PPBadgeViewFlexMode = .tail) {
  68. _bottomView.pp.setBadge(flexMode: flexMode)
  69. }
  70. /// 设置Badge的高度,因为Badge宽度是动态可变的,通过改变Badge高度,其宽度也按比例变化,方便布局
  71. ///
  72. /// (注意: 此方法需要将Badge添加到控件上后再调用!!!)
  73. ///
  74. /// Set the height of Badge, because the Badge width is dynamically and  variable.By changing the Badge height in proportion to facilitate the layout.
  75. ///
  76. /// (Note: this method needs to add Badge to the controls and then use it !!!)
  77. ///
  78. /// - Parameter height: 高度大小
  79. func setBadge(height: CGFloat) {
  80. _bottomView.pp.setBadge(height: height)
  81. }
  82. /// 显示Badge
  83. func showBadge() {
  84. _bottomView.pp.showBadge()
  85. }
  86. /// 隐藏Badge
  87. func hiddenBadge() {
  88. _bottomView.pp.hiddenBadge()
  89. }
  90. // MARK: - 数字增加/减少, 注意:以下方法只适用于Badge内容为纯数字的情况
  91. // MARK: - Digital increase /decrease, note: the following method applies only to cases where the Badge content is purely numeric
  92. /// badge数字加1
  93. func increase() {
  94. _bottomView.pp.increase()
  95. }
  96. /// badge数字加number
  97. func increaseBy(number: Int) {
  98. _bottomView.pp.increaseBy(number: number)
  99. }
  100. /// badge数字加1
  101. func decrease() {
  102. _bottomView.pp.decrease()
  103. }
  104. /// badge数字减number
  105. func decreaseBy(number: Int) {
  106. _bottomView.pp.decreaseBy(number: number)
  107. }
  108. /// 通过Xcode视图调试工具找到UITabBarItem原生Badge所在父视图
  109. private var _bottomView: UIView {
  110. let tabBarButton = (self.base.value(forKey: "_view") as? UIView) ?? UIView()
  111. for subView in tabBarButton.subviews {
  112. guard let superclass = subView.superclass else { return tabBarButton }
  113. if superclass == NSClassFromString("UIImageView") {
  114. return subView
  115. }
  116. }
  117. return tabBarButton
  118. }
  119. }