// // UIView+Utils.swift // MTP2_iOS // // Created by zhongyuan on 2018/3/19. // Copyright © 2018年 zhongyuan.All rights reserved. // import UIKit class ZJGetConstraint: NSObject { var firstItem:AnyObject? var constraints:Array = Array() var top:Array{ get { return self.constraints(firstAttributes: [NSLayoutConstraint.Attribute.top.rawValue,NSLayoutConstraint.Attribute.topMargin.rawValue]) } } var left:Array { get { return self.constraints(firstAttributes:[NSLayoutConstraint.Attribute.left.rawValue, NSLayoutConstraint.Attribute.leading.rawValue, NSLayoutConstraint.Attribute.leftMargin.rawValue]) } } var bottom:Array { get { return self.constraints(firstAttributes:[NSLayoutConstraint.Attribute.bottom.rawValue, NSLayoutConstraint.Attribute.bottomMargin.rawValue]) } } var right:Array { get { return self.constraints(firstAttributes:[NSLayoutConstraint.Attribute.right.rawValue, NSLayoutConstraint.Attribute.trailing.rawValue, NSLayoutConstraint.Attribute.trailingMargin.rawValue]) } } var width:Array { get { return self.constraints(firstAttributes:[NSLayoutConstraint.Attribute.width.rawValue]) } } var height:Array { get { return self.constraints(firstAttributes:[NSLayoutConstraint.Attribute.height.rawValue]) } } var centerX:Array { get { return self.constraints(firstAttributes:[NSLayoutConstraint.Attribute.centerX.rawValue, NSLayoutConstraint.Attribute.centerXWithinMargins.rawValue]) } } var centerY:Array { get{ return self.constraints(firstAttributes:[NSLayoutConstraint.Attribute.centerY.rawValue, NSLayoutConstraint.Attribute.centerYWithinMargins.rawValue]) } } func constraints(firstAttributes:Array) -> Array{ var lcArr = Array() for lc:NSLayoutConstraint in constraints { for firstAttribute in firstAttributes{ if ((lc.firstItem as? NSLayoutConstraint) == (firstItem as? NSLayoutConstraint) && lc.firstAttribute.rawValue == firstAttribute) { lcArr.append(lc) break } } } return lcArr } } extension UIView { /// 盘弹起时解决输入框被遮挡问题 /// /// - Parameter views: 需要使用软键盘输入的文本控件列表 func moveView(views: [UIView]) { NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillChangeFrameNotification, object: nil, queue: nil) { [weak self] (notification) in guard let weakSelf = self, let responseView = views.first(where: { $0.isFirstResponder }) else { return } /// 获取键盘的坐标 let endFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue let keyboardY: CGFloat = kScreenHeight - endFrame.origin.y /// 获取目标的Y值 let targetY: CGFloat = responseView.convert(CGPoint(x: 0, y: responseView.height), to: weakSelf).y /// 为防止遮挡需要偏移的距离 let offset = targetY + keyboardY - weakSelf.height + 24 /// 24是textfield和键盘上方的间距,可以自由设定 UIView.animate(withDuration: 0.25) { if offset > 0 { weakSelf.bounds = CGRect(x: 0, y: offset, width: weakSelf.width, height: weakSelf.height) } else { weakSelf.bounds = CGRect(x: 0, y: 0, width: weakSelf.width, height: weakSelf.height) } } } } /// 获取视图的父容器的方法 /// /// - Returns: 父容器 func superController() -> UIViewController? { var next: UIView? = self.superview repeat { if let nextResponder = next?.next { if nextResponder.isKind(of: UIViewController.classForCoder()) { return nextResponder as? UIViewController } } next = next?.superview } while next != nil return nil } } extension UIView { // MARK: - 添加渐变色图层 public func gradientColor(startPoint: CGPoint = .zero, endPoint: CGPoint = CGPoint(x: 1.0, y: 0.0), _ colors: [Any], _ cornerRadius: CGFloat, _ maskedCorners: CACornerMask?) { self.layer.cornerRadius = cornerRadius if let corners = maskedCorners { self.layer.maskedCorners = corners } /// 新建一个渐变层 let layer = CAGradientLayer() layer.frame = self.layer.bounds /// 将渐变层的颜色属性设置为由三个颜色所构建的数组 layer.colors = colors layer.cornerRadius = cornerRadius if let corners = maskedCorners { layer.maskedCorners = corners } layer.startPoint = startPoint layer.endPoint = endPoint /// 将设置好的渐变层添加到视图对象的层 self.layer.insertSublayer(layer, at: 0) } // MARK: - 移除渐变图层 /// (当希望只使用backgroundColor的颜色时,需要先移除之前加过的渐变图层) public func removeGradientLayer() { if let sl = self.layer.sublayers { for layer in sl { if layer.isKind(of: CAGradientLayer.self) { layer.removeFromSuperlayer() } } } } }