123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- import UIKit
- extension UIView {
- public var x: CGFloat {
- get {
- return frame.origin.x
- }
- set {
- frame = CGRect(x: newValue, y: y, width: width, height: height)
- }
- }
-
- public var y: CGFloat {
- get {
- return frame.origin.y
- }
- set {
- frame = CGRect(x: x, y: newValue, width: width, height: height)
- }
- }
-
- public var width: CGFloat {
- get {
- return frame.width
- }
- set {
- frame = CGRect(x: x, y: y, width: newValue, height: height)
- }
- }
-
- public var height: CGFloat {
- get {
- return frame.height
- }
- set {
- frame = CGRect(x: x, y: y, width: width, height: newValue)
- }
- }
-
- public var size: CGSize {
- get {
- return frame.size
- }
- set {
- frame = CGRect(x: x, y: y, width: newValue.width, height: newValue.height)
- }
- }
-
- public var origin: CGPoint {
- get {
- return frame.origin
- }
- set {
- frame = CGRect(x: origin.x, y: origin.y, width: width, height: height)
- }
- }
- }
- extension UIView {
- @IBInspectable public var borderWidth: CGFloat {
- get {
- return self.layer.borderWidth
- }
- set {
- self.layer.borderWidth = newValue
- self.layer.borderColor = UIColor.lightGray.cgColor
- }
-
- }
- @IBInspectable public var cornerRadius: CGFloat {
- get {
- return self.layer.cornerRadius
- }
- set {
- self.layer.masksToBounds = true
- self.layer.cornerRadius = newValue
- }
- }
- @IBInspectable public var borderColor: UIColor? {
- get {
- guard let color = self.layer.borderColor else {
- return nil
- }
- return UIColor(cgColor: color)
- }
- set {
- if let color = newValue {
- self.layer.borderColor = color.cgColor
- }
- }
- }
-
- @IBInspectable
- public var shadowColor: UIColor? {
- get {
- guard let color = layer.shadowColor else {
- return nil
- }
- return UIColor(cgColor: color)
- }
- set {
- if let color = newValue {
- layer.shadowColor = color.cgColor
- }
-
- }
- }
-
- @IBInspectable
- public var shadowOffset: CGSize {
- get {
- return layer.shadowOffset
- }
- set {
- layer.shadowOffset = newValue
- }
- }
-
- @IBInspectable
- public var shadowOpacity: Float {
- get {
- return layer.shadowOpacity
- }
- set {
- layer.shadowOpacity = newValue
- }
- }
- }
- extension UIView {
- public var screenshot: UIImage? {
- UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, UIScreen.main.scale)
- defer {
- UIGraphicsEndImageContext()
- }
- guard let content = UIGraphicsGetCurrentContext() else {
- return nil
- }
- layer.render(in: content)
- return UIGraphicsGetImageFromCurrentImageContext()
- }
-
- public var visiable: Bool {
- return !isHidden && alpha > 0 && window != nil
- }
- }
- extension UIView {
- func getSuperViewController() -> UIViewController? {
- var nr = next
- while let r = nr {
- if let vc = r as? UIViewController { return vc }
- nr = r.next
- }
-
- return nil
- }
- }
|