在開發(fā)的時候,給控件和界面布局的一直使用CGRectMake椅您,關于CGRect其他的方法比較少用西轩,今天認真的去看了一下CGRect的其他方法马僻,才發(fā)現(xiàn)還有很多方法對純代碼布局是很有用的韭邓。特別是下面這個幾個瞭郑,對于方法的作用,在代碼塊中又注釋阁谆。在swift里,這幾個方法變成了屬性裳凸,同學們在調用的時候注意一點映九。
CGRectGetMinX,CGRectGetMaxX,CGRectGetMinY,CGRectGetMaxY,CGRectGetMidX,CGRectGetMidY
關于OC:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
view.backgroundColor = [UIColor greenColor];
[self.view addSubview:view];
//獲取view的最小X值捌议,也就是這個view的X值
CGFloat minX = CGRectGetMinX(view.frame);
NSLog(@"minx is %f",minX);
//獲取view的最大的X值譬正,也就是這個view的X值+view的寬
CGFloat maxX = CGRectGetMaxX(view.frame);
NSLog(@"maxX is %f",maxX);
//獲取view的最小Y值粉怕,也就是這個view的Y值
CGFloat minY = CGRectGetMinY(view.frame);
NSLog(@"minY is %f",minY);
//獲取view的最大Y值贫贝,也就是這個view的Y值+view的高度
CGFloat maxY = CGRectGetMaxY(view.frame);
NSLog(@"maxY is %f",maxY);
//獲取view的中點X
CGFloat midX = CGRectGetMidX(view.frame);
NSLog(@"midX is %f",midX);
//獲取view的中點Y
CGFloat midY = CGRectGetMidY(view.frame);
NSLog(@"midY is %f",midY);
//獲取view的高
CGFloat getWidth = CGRectGetWidth(view.frame);
NSLog(@"getWidth is %f",getWidth);
//獲取view的寬
CGFloat getHeight = CGRectGetHeight(view.frame);
NSLog(@"getHeight is %f",getHeight);
//判斷兩個view大小是否一樣
BOOL flag = CGRectEqualToRect(view.frame, self.view.frame);
NSLog(@"flag is %d",flag);
//
CGRect rect = CGRectStandardize(view.frame);
NSLog(@"rect x = %f, y = %f, width = %f, height = %f",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
//判斷view的rect是否為空
BOOL flag1 = CGRectIsNull(view.frame);
NSLog(@"flag is %d",flag1);
//斷view的rect是否為空
BOOL flag2 = CGRectIsEmpty(view.frame);
NSLog(@"flag2 is %d",flag2);
//基于某個view蜈彼,傳入X和Y,獲取一個新的rect。
//官方文檔給出的解釋是另個view是否相交
CGRect smallRect = CGRectInset(view.frame, -10, 10);
NSLog(@"%f,%f,%f,%f",smallRect.origin.x,smallRect.origin.y,smallRect.size.width,smallRect.size.height);
UIView *view1 = [[UIView alloc]initWithFrame:smallRect];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
}
圖片是CGRectInset這個方法的效果
關于Swift:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
let greenView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
greenView.backgroundColor = UIColor.green
view.addSubview(greenView)
let minX: CGFloat = greenView.frame.minX
print("minX is \(minX)")
let maxX: CGFloat = greenView.frame.maxX
print("maxX is \(maxX)")
let minY: CGFloat = greenView.frame.minY
print("minY is \(minY)")
let maxY: CGFloat = greenView.frame.maxY
print("maxY is \(maxY)")
let midX: CGFloat = greenView.frame.midX
print("midX is \(midX)")
let midY: CGFloat = greenView.frame.midY
print("midY is \(midY)")
let width: CGFloat = greenView.frame.width
print("width is \(width)")
let height: CGFloat = greenView.frame.height
print("height is \(height)")
let rect: CGRect = greenView.frame.insetBy(dx: 10, dy: 10)
let redView = UIView(frame: rect)
redView.backgroundColor = UIColor.red
view.addSubview(redView)
}
啊啊啊踏揣,感覺自己坑了自己一大波捞稿,走了巨大段的彎路,還是怪自己看官方文檔看的少啊衰齐。