-
Notifications
You must be signed in to change notification settings - Fork 4
/
UIView+Extension.swift
168 lines (151 loc) · 3.89 KB
/
UIView+Extension.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// UIView+Extension.swift
// WLMall1
//
// Created by 陈波 on 2017/7/26.
// Copyright © 2017年 WulianWisdom. All rights reserved.
//
import UIKit
enum Direction {
case top
case left
case bottom
case right
}
extension UIView {
/// 尺寸
var size: CGSize {
get {
return self.frame.size
}
set(newValue) {
self.frame.size = CGSize(width: newValue.width, height: newValue.height)
}
}
/// 宽度
var width: CGFloat {
get {
return self.frame.size.width
}
set(newValue) {
self.frame.size.width = newValue
}
}
/// 高度
var height: CGFloat {
get {
return self.frame.size.height
}
set(newValue) {
self.frame.size.height = newValue
}
}
/// 横坐标
var x: CGFloat {
get {
return self.frame.minX
}
set(newValue) {
self.frame = CGRect(x: newValue, y: y, width: width, height: height)
}
}
/// 纵坐标
var y: CGFloat {
get {
return self.frame.minY
}
set(newValue) {
self.frame = CGRect(x: x, y: newValue, width: width, height: height)
}
}
/// 右端横坐标
var right: CGFloat {
get {
return frame.origin.x + frame.size.width
}
set(newValue) {
frame.origin.x = newValue - frame.size.width
}
}
/// 底端纵坐标
var bottom: CGFloat {
get {
return frame.origin.y + frame.size.height
}
set(newValue) {
frame.origin.y = newValue - frame.size.height
}
}
/// 中心横坐标
var centerX: CGFloat {
get {
return self.center.x
}
set(newValue) {
center.x = newValue
}
}
/// 中心纵坐标
var centerY: CGFloat {
get {
return center.y
}
set(newValue) {
center.y = newValue
}
}
/// 原点
var origin: CGPoint {
get {
return self.origin
}
set(newValue) {
frame.origin = newValue
}
}
/// 右上角坐标
var topRight: CGPoint {
get {
return CGPoint(x: frame.origin.x + frame.size.width, y: frame.origin.y)
}
set(newValue) {
frame.origin = CGPoint(x: newValue.x - width, y: newValue.y)
}
}
/// 右下角坐标
var bottomRight: CGPoint {
get {
return CGPoint(x: frame.origin.x + frame.size.width, y: frame.origin.y + frame.size.height)
}
set(newValue) {
frame.origin = CGPoint(x: newValue.x - width, y: newValue.y - height)
}
}
/// 左下角坐标
var bottomLeft: CGPoint {
get {
return CGPoint(x: frame.origin.x, y: frame.origin.y + frame.size.height)
}
set(newValue) {
frame.origin = CGPoint(x: newValue.x, y: newValue.y - height)
}
}
/// 获取UIView对象某个方向缩进指定距离后的方形区域
///
/// - Parameters:
/// - direction: 要缩进的方向
/// - distance: 缩进的距离
/// - Returns: 得到的区域
func cutRect(direction: Direction, distance: CGFloat) -> CGRect {
switch direction {
case .top:
return CGRect(x: 0, y: distance, width: self.width, height: self.height - distance)
case .left:
return CGRect(x: distance, y: 0, width: self.width - distance, height: self.height)
case .right:
return CGRect(x: 0, y: 0, width: self.width - distance, height: self.height)
case .bottom:
return CGRect(x: 0, y: 0, width: self.width, height: self.height - distance)
}
}
}