// // IBView.swift // MTP2_iOS // // Created by zhongyuan on 2018/3/17. // Copyright © 2018年 zhongyuan.All rights reserved. // import UIKit @IBDesignable class IBView: UIView { @IBInspectable var 圆角偏移量:CGFloat = 0.0 @IBInspectable var 圆角倍数:CGPoint = CGPoint.init(x: 0, y: 0) @IBInspectable var maskToBounds:Bool = false @IBInspectable var borderColor:UIColor = UIColor.clear @IBInspectable var borderWidth:CGFloat = 0.0 @IBInspectable var shadowOffset: CGSize = CGSize.zero { didSet { self.layer.shadowOffset = shadowOffset } } @IBInspectable var shadowColor: UIColor = UIColor.clear { didSet { self.layer.shadowColor = shadowColor.cgColor } } @IBInspectable var shadowRadius: CGFloat = 0 { didSet { self.layer.shadowRadius = shadowRadius } } @IBInspectable var shadowOpacity: Float = 0 { didSet { self.layer.shadowOpacity = shadowOpacity } } @IBInspectable var 渐变颜色: UIColor = UIColor.white { didSet { let layer = CAGradientLayer() layer.colors = [渐变颜色.cgColor, UIColorFromHex(rgbValue: 0xF5594B,alpha: 0.5).cgColor] layer.frame = self.bounds layer.locations = [0.1] layer.startPoint = CGPoint(x: 0, y: 0) layer.endPoint = CGPoint(x: 0, y: 1) self.layer.addSublayer(layer) } } static func refreshIBEffect(view:UIView){ /**< 刷新 IBInspectable 的效果 */ guard let 圆角倍数:CGPoint = view.value(forKey:"圆角倍数") as? CGPoint, let 圆角偏移量:CGFloat = view.value(forKey:"圆角偏移量") as? CGFloat, let maskToBounds:Bool = view.value(forKey:"maskToBounds") as? Bool, let borderColor:UIColor = view.value(forKey:"borderColor") as? UIColor, let borderWidth:Double = view.value(forKey:"borderWidth") as? Double else { return } let cornerRadiusRatio: CGPoint = 圆角倍数 let cornerRadiusOffset: CGFloat = 圆角偏移量 view.layer.cornerRadius = cornerRadiusRatio.x * view.frame.size.width + cornerRadiusRatio.y * view.frame.size.height + cornerRadiusOffset view.layer.masksToBounds = maskToBounds view.layer.borderColor = borderColor.cgColor view.layer.borderWidth = CGFloat(borderWidth) view.layer.sublayers?.forEach({ if $0.isMember(of: CAGradientLayer.self) { $0.frame = view.frame } }) } override func layoutSubviews() { super.layoutSubviews() IBView.refreshIBEffect(view: self) } } @IBDesignable class IBLabel:UILabel{ @IBInspectable var 圆角偏移量:CGFloat = 0.0 @IBInspectable var 圆角倍数:CGPoint = CGPoint.init(x: 0, y: 0) @IBInspectable var maskToBounds:Bool = false @IBInspectable var borderColor:UIColor = UIColor.clear @IBInspectable var borderWidth:CGFloat = 0.0 private var padding = UIEdgeInsets.zero @IBInspectable var paddingLeft: CGFloat { get { return padding.left } set { padding.left = newValue } } @IBInspectable var paddingRight: CGFloat { get { return padding.right } set { padding.right = newValue } } @IBInspectable var paddingTop: CGFloat { get { return padding.top } set { padding.top = newValue } } @IBInspectable var paddingBottom: CGFloat { get { return padding.bottom } set { padding.bottom = newValue } } override func drawText(in rect: CGRect) { super.drawText(in: (CGRect.inset(rect))(by: padding)) //UIEdgeInsetsInsetRect(rect, padding)) } override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect { let insets = self.padding var rect = super.textRect(forBounds: (CGRect.inset(bounds))(by: insets), limitedToNumberOfLines: numberOfLines) rect.origin.x -= insets.left rect.origin.y -= insets.top rect.size.width += (insets.left + insets.right) rect.size.height += (insets.top + insets.bottom) return rect } override func layoutSubviews() { super.layoutSubviews() IBView.refreshIBEffect(view: self) } } @IBDesignable class IBImageView: UIImageView { @IBInspectable var 圆角偏移量:CGFloat = 0.0 @IBInspectable var 圆角倍数:CGPoint = CGPoint.init(x: 0, y: 0) @IBInspectable var maskToBounds:Bool = false @IBInspectable var borderColor:UIColor = UIColor.clear @IBInspectable var borderWidth:CGFloat = 0.0 override func layoutSubviews() { super.layoutSubviews() IBView.refreshIBEffect(view: self) } } fileprivate var backGroundColorString = "backGroundColorString" @IBDesignable extension UIView { var x: CGFloat { get { return frame.origin.x } set(newValue) { var tmpFrame : CGRect = frame tmpFrame.origin.x = newValue frame = tmpFrame } } var y: CGFloat { get { return frame.origin.y } set(newValue) { var tmpFrame : CGRect = frame tmpFrame.origin.y = newValue frame = tmpFrame } } var height: CGFloat { get { return frame.size.height } set(newValue) { var tmpFrame : CGRect = frame tmpFrame.size.height = newValue frame = tmpFrame } } var width: CGFloat { get { return frame.size.width } set(newValue) { var tmpFrame: CGRect = frame tmpFrame.size.width = newValue frame = tmpFrame } } var left: CGFloat { get { return x } set(newValue) { x = newValue } } var right: CGFloat { get { return x + width } set(newVal) { x = newVal - width } } var top: CGFloat { get { return y } set(newVal) { y = newVal } } var bottom: CGFloat { get { return y + height } set(newVal) { y = newVal - height } } var centerX: CGFloat { get { return center.x } set(newVal) { center = CGPoint(x: newVal, y: center.y) } } var centerY: CGFloat { get { return center.y } set(newVal) { center = CGPoint(x: center.x, y: newVal) } } var middleX: CGFloat { get { return width / 2 } } var middleY: CGFloat { get { return height / 2 } } var middlePoint: CGPoint { get { return CGPoint(x: middleX, y: middleY) } } /// SlideSegmentView调用 var bx : CGFloat { return self.x + self.width } /// 是否参与颜色修改 @IBInspectable var colorFlag: Bool { get { return objc_getAssociatedObject(self, &backGroundColorString) as! Bool } set { objc_setAssociatedObject(self, &backGroundColorString, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } } } fileprivate var backGroundColorAnim = "backGroundColorAnim" fileprivate var backGroundColorAnimColor = "backGroundColorAnimColor" extension UIView { @IBInspectable var showTapAnim: Bool { get { return objc_getAssociatedObject(self, &backGroundColorAnim) as! Bool } set { objc_setAssociatedObject(self, &backGroundColorAnim, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) if newValue { allCanTap(sender: self) let tap = UITapGestureRecognizer(target: self, action: #selector(tapAnimation)) self.addGestureRecognizer(tap) } } } @IBInspectable var tapAnimColor: UIColor { get { return objc_getAssociatedObject(self, &backGroundColorAnimColor) as! UIColor } set { objc_setAssociatedObject(self, &backGroundColorAnimColor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } } /// 设置该view所有的子级view都可以点击 /// /// - Parameter sender: 需要设置的view private func allCanTap(sender: UIView) { if let sender = sender as? UIStackView { sender.arrangedSubviews.forEach({ $0.isUserInteractionEnabled = true allCanTap(sender: $0) }) } else { sender.subviews.forEach({ $0.isUserInteractionEnabled = true allCanTap(sender: $0) }) } } @objc private func tapAnimation() { let tmpView = UIView(frame: self.bounds) tmpView.backgroundColor = tapAnimColor.withAlphaComponent(0.3) tmpView.alpha = 0 self.addSubview(tmpView) UIView.animate(withDuration: 0.20, delay: 0, options: .curveEaseIn, animations: { tmpView.alpha = 0.8 }) { (finished) in UIView.animate(withDuration: 0.20, delay: 0.1, options: .curveEaseOut, animations: { tmpView.alpha = 0 }, completion: { (finished) in tmpView.removeFromSuperview() }) } } } extension UIColor { /** Create a ligher color */ func lighter(by percentage: CGFloat = 30.0) -> UIColor { return self.adjustBrightness(by: abs(percentage)) } /** Create a darker color */ func darker(by percentage: CGFloat = 30.0) -> UIColor { return self.adjustBrightness(by: -abs(percentage)) } /** Try to increase brightness or decrease saturation */ func adjustBrightness(by percentage: CGFloat = 30.0) -> UIColor { var h: CGFloat = 0, s: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0 if self.getHue(&h, saturation: &s, brightness: &b, alpha: &a) { if b < 1.0 { let newB: CGFloat = max(min(b + (percentage/100.0)*b, 1.0), 0,0) return UIColor(hue: h, saturation: s, brightness: newB, alpha: a) } else { let newS: CGFloat = min(max(s - (percentage/100.0)*s, 0.0), 1.0) return UIColor(hue: h, saturation: newS, brightness: b, alpha: a) } } return self } }