UIView+Utils.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // UIView+Utils.swift
  3. // MTP2_iOS
  4. //
  5. // Created by zhongyuan on 2018/3/19.
  6. // Copyright © 2018年 zhongyuan.All rights reserved.
  7. //
  8. import UIKit
  9. class ZJGetConstraint: NSObject {
  10. var firstItem:AnyObject?
  11. var constraints:Array<NSLayoutConstraint> = Array<NSLayoutConstraint>()
  12. var top:Array<NSLayoutConstraint>{
  13. get { return self.constraints(firstAttributes: [NSLayoutConstraint.Attribute.top.rawValue,NSLayoutConstraint.Attribute.topMargin.rawValue]) }
  14. }
  15. var left:Array<NSLayoutConstraint> {
  16. get {
  17. return self.constraints(firstAttributes:[NSLayoutConstraint.Attribute.left.rawValue,
  18. NSLayoutConstraint.Attribute.leading.rawValue,
  19. NSLayoutConstraint.Attribute.leftMargin.rawValue])
  20. }
  21. }
  22. var bottom:Array<NSLayoutConstraint> {
  23. get {
  24. return self.constraints(firstAttributes:[NSLayoutConstraint.Attribute.bottom.rawValue,
  25. NSLayoutConstraint.Attribute.bottomMargin.rawValue])
  26. }
  27. }
  28. var right:Array<NSLayoutConstraint> {
  29. get {
  30. return self.constraints(firstAttributes:[NSLayoutConstraint.Attribute.right.rawValue,
  31. NSLayoutConstraint.Attribute.trailing.rawValue,
  32. NSLayoutConstraint.Attribute.trailingMargin.rawValue])
  33. }
  34. }
  35. var width:Array<NSLayoutConstraint> {
  36. get {
  37. return self.constraints(firstAttributes:[NSLayoutConstraint.Attribute.width.rawValue])
  38. }
  39. }
  40. var height:Array<NSLayoutConstraint> {
  41. get {
  42. return self.constraints(firstAttributes:[NSLayoutConstraint.Attribute.height.rawValue])
  43. }
  44. }
  45. var centerX:Array<NSLayoutConstraint> {
  46. get {
  47. return self.constraints(firstAttributes:[NSLayoutConstraint.Attribute.centerX.rawValue,
  48. NSLayoutConstraint.Attribute.centerXWithinMargins.rawValue])
  49. }
  50. }
  51. var centerY:Array<NSLayoutConstraint> {
  52. get{
  53. return self.constraints(firstAttributes:[NSLayoutConstraint.Attribute.centerY.rawValue,
  54. NSLayoutConstraint.Attribute.centerYWithinMargins.rawValue])
  55. }
  56. }
  57. func constraints(firstAttributes:Array<Int>) -> Array<NSLayoutConstraint>{
  58. var lcArr = Array<NSLayoutConstraint>()
  59. for lc:NSLayoutConstraint in constraints {
  60. for firstAttribute in firstAttributes{
  61. if ((lc.firstItem as? NSLayoutConstraint) == (firstItem as? NSLayoutConstraint)
  62. && lc.firstAttribute.rawValue == firstAttribute) {
  63. lcArr.append(lc)
  64. break
  65. }
  66. }
  67. }
  68. return lcArr
  69. }
  70. }
  71. extension UIView {
  72. /// 盘弹起时解决输入框被遮挡问题
  73. ///
  74. /// - Parameter views: 需要使用软键盘输入的文本控件列表
  75. func moveView(views: [UIView]) {
  76. NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillChangeFrameNotification, object: nil, queue: nil) { [weak self] (notification) in
  77. guard let weakSelf = self, let responseView = views.first(where: { $0.isFirstResponder }) else { return }
  78. /// 获取键盘的坐标
  79. let endFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
  80. let keyboardY: CGFloat = kScreenHeight - endFrame.origin.y
  81. /// 获取目标的Y值
  82. let targetY: CGFloat = responseView.convert(CGPoint(x: 0, y: responseView.height), to: weakSelf).y
  83. /// 为防止遮挡需要偏移的距离
  84. let offset = targetY + keyboardY - weakSelf.height + 24 /// 24是textfield和键盘上方的间距,可以自由设定
  85. UIView.animate(withDuration: 0.25) {
  86. if offset > 0 {
  87. weakSelf.bounds = CGRect(x: 0, y: offset, width: weakSelf.width, height: weakSelf.height)
  88. } else {
  89. weakSelf.bounds = CGRect(x: 0, y: 0, width: weakSelf.width, height: weakSelf.height)
  90. }
  91. }
  92. }
  93. }
  94. /// 获取视图的父容器的方法
  95. ///
  96. /// - Returns: 父容器
  97. func superController() -> UIViewController? {
  98. var next: UIView? = self.superview
  99. repeat {
  100. if let nextResponder = next?.next {
  101. if nextResponder.isKind(of: UIViewController.classForCoder()) {
  102. return nextResponder as? UIViewController
  103. }
  104. }
  105. next = next?.superview
  106. } while next != nil
  107. return nil
  108. }
  109. }
  110. extension UIView {
  111. // MARK: - 添加渐变色图层
  112. public func gradientColor(startPoint: CGPoint = .zero, endPoint: CGPoint = CGPoint(x: 1.0, y: 0.0), _ colors: [Any], _ cornerRadius: CGFloat, _ maskedCorners: CACornerMask?) {
  113. self.layer.cornerRadius = cornerRadius
  114. if let corners = maskedCorners { self.layer.maskedCorners = corners }
  115. /// 新建一个渐变层
  116. let layer = CAGradientLayer()
  117. layer.frame = self.layer.bounds
  118. /// 将渐变层的颜色属性设置为由三个颜色所构建的数组
  119. layer.colors = colors
  120. layer.cornerRadius = cornerRadius
  121. if let corners = maskedCorners { layer.maskedCorners = corners }
  122. layer.startPoint = startPoint
  123. layer.endPoint = endPoint
  124. /// 将设置好的渐变层添加到视图对象的层
  125. self.layer.insertSublayer(layer, at: 0)
  126. }
  127. // MARK: - 移除渐变图层
  128. /// (当希望只使用backgroundColor的颜色时,需要先移除之前加过的渐变图层)
  129. public func removeGradientLayer() {
  130. if let sl = self.layer.sublayers {
  131. for layer in sl {
  132. if layer.isKind(of: CAGradientLayer.self) {
  133. layer.removeFromSuperlayer()
  134. }
  135. }
  136. }
  137. }
  138. }