AppUpdateAlert.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import Foundation
  2. import UIKit
  3. public func kRGB(r:CGFloat, g:CGFloat, b:CGFloat) ->UIColor{return UIColor (red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1.0)}
  4. ///屏幕宽
  5. let kSCREEN_WIDTH = UIScreen.main.bounds.width
  6. ///屏幕高
  7. let kSCREEN_HEIGHT = UIScreen.main.bounds.height
  8. let Ratio_375 = kSCREEN_WIDTH / 375.0
  9. /// 转换成当前比例的数
  10. func Ratio(x:CGFloat) -> CGFloat {
  11. return CGFloat(x * Ratio_375)
  12. }
  13. let DEFAULT_MAX_HEIGHT = kSCREEN_HEIGHT / 3 * 2
  14. class AppUpdateAlert: UIView {
  15. /** 版本号 */
  16. var version = ""
  17. /** 版本更新内容 */
  18. var desc = ""
  19. /// 升级地址
  20. var updateUrl: String = ""
  21. class func showUpdateAlert(version: String?, description: String?, _ trackViewUrl: String?) {
  22. let updateAlert = AppUpdateAlert.init(version: version!, description: description!, trackViewUrl)
  23. UIApplication.shared.delegate?.window??.addSubview(updateAlert)
  24. }
  25. override init(frame: CGRect) {
  26. super.init(frame: frame)
  27. }
  28. required init?(coder aDecoder: NSCoder) {
  29. fatalError("init(coder:) has not been implemented")
  30. }
  31. init(version: String, description:String, _ trackViewUrl: String?) {
  32. super.init(frame: UIScreen.main.bounds)
  33. /// 版本号
  34. self.version = version
  35. /// 更新信息
  36. desc = description
  37. /// 升级地址
  38. updateUrl = trackViewUrl ?? ""
  39. /// 初始化
  40. initView()
  41. }
  42. }
  43. // MARK: - init
  44. extension AppUpdateAlert {
  45. func initView() {
  46. self.frame = UIScreen.main.bounds
  47. self.backgroundColor = UIColor(red: CGFloat(0 / 255.0), green: CGFloat(0 / 255.0), blue: CGFloat(0 / 255.0), alpha: 0.3 / 1.0)
  48. //获取更新内容高度
  49. var descHeight = _sizeofString(desc, font: UIFont.systemFont(ofSize: 16), maxSize: CGSize(width: self.frame.size.width-Ratio(x: 136), height: 1000)).height
  50. //bgView实际高度
  51. let realHeight: CGFloat = descHeight + Ratio(x: 314)
  52. //bgView最大高度
  53. var maxHeight: CGFloat = DEFAULT_MAX_HEIGHT
  54. //更新内容可否滑动显示
  55. var scrollEnabled = false
  56. //重置bgView最大高度 设置更新内容可否滑动显示
  57. if realHeight > DEFAULT_MAX_HEIGHT {
  58. scrollEnabled = true
  59. descHeight = DEFAULT_MAX_HEIGHT - Ratio(x: 314)
  60. } else {
  61. maxHeight = realHeight
  62. }
  63. let bgView = UIView()
  64. bgView.center = center
  65. bgView.bounds = CGRect(x: 0, y: 0, width: frame.size.width - Ratio(x: 40), height: maxHeight + Ratio(x: 18))
  66. addSubview(bgView)
  67. //添加更新提示
  68. let updateView = UIView(frame: CGRect(x: Ratio(x: 20), y: Ratio(x: 18), width: bgView.frame.size.width - Ratio(x: 40), height: maxHeight))
  69. updateView.backgroundColor = UIColor.init(white: 1, alpha: 0);
  70. bgView.addSubview(updateView)
  71. let updatebg_image = UIImageView(frame: CGRect(x: 0, y: 0, width: updateView.frame.size.width, height: updateView.frame.size.height))
  72. updatebg_image.image = UIImage(named: "update_bg")
  73. updateView.addSubview(updatebg_image)
  74. let updateIcon = UIImageView(frame: CGRect(x: (updateView.frame.size.width/8)*3, y: -Ratio(x: (updateView.frame.size.width)/4), width: Ratio(x: (updateView.frame.size.width)/4), height: Ratio(x: (updateView.frame.size.width)/2)))
  75. updateIcon.image = UIImage(named: "update_huojian")
  76. updateView.addSubview(updateIcon)
  77. //版本号
  78. let versionLabel = UILabel(frame: CGRect(x: Ratio(x:25), y: Ratio(x:160), width: updateView.frame.size.width, height: Ratio(x:28)))
  79. versionLabel.font = UIFont.boldSystemFont(ofSize: 16)
  80. versionLabel.textAlignment = .left
  81. versionLabel.text = "发现新版本 V\(version)"
  82. updateView.addSubview(versionLabel)
  83. let descTextView = UITextView(frame: CGRect(x: Ratio(x: 28), y: Ratio(x: 10) + versionLabel.frame.maxY, width: updateView.frame.size.width - Ratio(x:56), height: descHeight))
  84. descTextView.font = UIFont.systemFont(ofSize: CGFloat(16))
  85. descTextView.textContainer.lineFragmentPadding = 0
  86. descTextView.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  87. descTextView.text = desc
  88. descTextView.isEditable = false
  89. descTextView.isSelectable = false
  90. descTextView.isScrollEnabled = scrollEnabled
  91. descTextView.showsVerticalScrollIndicator = scrollEnabled
  92. descTextView.showsHorizontalScrollIndicator = false
  93. updateView.addSubview(descTextView)
  94. if scrollEnabled {
  95. //若显示滑动条,提示可以有滑动条
  96. descTextView.flashScrollIndicators()
  97. }
  98. //更新按钮
  99. let updateButton = UIButton(type: .system)
  100. updateButton.backgroundColor = kRGB(r: 239, g: 33, b: 67)
  101. updateButton.frame = CGRect(x: Ratio(x:25), y: descTextView.frame.maxY + Ratio(x:20), width: updateView.frame.size.width - Ratio(x:50), height: Ratio(x:40))
  102. updateButton.clipsToBounds = true
  103. updateButton.layer.cornerRadius = 2.0
  104. updateButton.addTarget(self, action: #selector(self.updateVersion), for: .touchUpInside)
  105. updateButton.setTitle("立即更新", for: .normal)
  106. updateButton.setTitleColor(UIColor.white, for: .normal)
  107. updateView.addSubview(updateButton)
  108. //取消按钮
  109. let cancelButton = UIButton(type: .system)
  110. cancelButton.backgroundColor = UIColor.white
  111. cancelButton.frame = CGRect(x: Ratio(x: 25), y: updateButton.frame.maxY+Ratio(x:5), width: updateView.frame.size.width - Ratio(x:50), height: Ratio(x:30))
  112. cancelButton.setTitle("取消", for: .normal)
  113. cancelButton.setTitleColor(UIColor.darkGray, for: .normal)
  114. cancelButton.addTarget(self, action: #selector(self.cancelAction), for: .touchUpInside)
  115. updateView.addSubview(cancelButton)
  116. //显示更新
  117. show(withAlert: bgView)
  118. }
  119. }
  120. // MARK: - action
  121. extension AppUpdateAlert{
  122. /// 更新按钮点击事件 跳转AppStore更新
  123. @objc func updateVersion() {
  124. if let aStr = URL(string: updateUrl) {
  125. if UIApplication.shared.canOpenURL(aStr) {
  126. UIApplication.shared.open(aStr, options: [:]) { (isFinish) in }
  127. }
  128. }
  129. }
  130. /// 取消按钮点击事件
  131. @objc func cancelAction() {
  132. dismissAlert()
  133. }
  134. /// 添加Alert入场动画
  135. ///
  136. /// - Parameter alert: 添加动画的View
  137. func show(withAlert alert: UIView?) {
  138. let animation = CAKeyframeAnimation(keyPath: "transform")
  139. animation.duration = CFTimeInterval(0.3)
  140. var values = [AnyHashable]()
  141. values.append(NSValue(caTransform3D: CATransform3DMakeScale(0.1, 0.1, 1.0)))
  142. values.append(NSValue(caTransform3D: CATransform3DMakeScale(1.2, 1.2, 1.0)))
  143. values.append(NSValue(caTransform3D: CATransform3DMakeScale(0.9, 0.9, 1.0)))
  144. values.append(NSValue(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)))
  145. animation.values = values
  146. alert?.layer.add(animation, forKey: nil)
  147. }
  148. /// 添加Alert出场动画
  149. func dismissAlert() {
  150. UIView.animate(withDuration: 0.3, animations: {() -> Void in
  151. self.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
  152. self.backgroundColor = UIColor.clear
  153. self.alpha = 0
  154. }, completion: {(_ finished: Bool) -> Void in
  155. self.removeFromSuperview()
  156. })
  157. }
  158. func _sizeofString(_ string: String?, font: UIFont?, maxSize: CGSize) -> CGSize {
  159. return string?.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedString.Key.font: font as Any], context: nil).size ?? CGSize.zero
  160. }
  161. }