iOS開發 ☞ 各種CG結構體
阿新 • • 發佈:2019-02-20
1、CGRectInset(<#CGRect rect#>, <#CGFloat dx#>, <#CGFloat dy#>)
中心相同
CG_EXTERN CGRect CGRectInset(CGRect rect, CGFloat dx, CGFloat dy)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor orangeColor];
UIView *otherView = [[UIView alloc] init];
otherView.frame = CGRectInset(view.frame, 2, 4);
otherView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
[self.view addSubview:otherView];
2、CGRectOffset(<#CGRect rect#>, <#CGFloat dx#>, <#CGFloat dy#>)
位置不同
CG_EXTERN CGRect CGRectOffset(CGRect rect, CGFloat dx, CGFloat dy)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor purpleColor];
UIView *otherView = [[UIView alloc] init];
otherView.frame = CGRectOffset(view.frame, 2, 4);
otherView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
[self.view addSubview:otherView];
3、CGRectGetMidY(<#CGRect rect#>) 、 CGRectGetMidX(<#CGRect rect#>)
獲取中心點座標
CG_EXTERN CGFloat CGRectGetMidX(CGRect rect)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
CG_EXTERN CGFloat CGRectGetMidY(CGRect rect)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor purpleColor];
UIView *otherView = [[UIView alloc] init];
otherView.frame = CGRectOffset(view.frame, 2, 4);
otherView.backgroundColor = [UIColor yellowColor];
CGFloat centerX = CGRectGetMidX(view.frame);
CGFloat centerY = CGRectGetMidX(view.frame);
NSLog(@"%f",centerX);
NSLog(@"%f",centerY);
[self.view addSubview:view];
[self.view addSubview:otherView];
4、CGRectIntersectsRect(<#CGRect rect1#>, <#CGRect rect2#>)
是否相交
In English,intersect means 交集
CG_EXTERN bool CGRectIntersectsRect(CGRect rect1, CGRect rect2)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor purpleColor];
UIView *otherView = [[UIView alloc] init];
otherView.frame = CGRectOffset(view.frame, 2, 4);
otherView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
[self.view addSubview:otherView];
BOOL isIntersect = CGRectIntersectsRect(view.frame, otherView.frame);
NSLog(@"%d",isIntersect);
5、CGRectIntersection(<#CGRect r1#>, <#CGRect r2#>)
相交區域
CG_EXTERN CGRect CGRectIntersection(CGRect r1, CGRect r2)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor purpleColor];
UIView *otherView = [[UIView alloc] init];
otherView.frame = CGRectOffset(view.frame, 2, 4);
otherView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
[self.view addSubview:otherView];
CGRect rect = CGRectIntersection(view.frame, otherView.frame);
NSLog(@"%@",NSStringFromCGRect(rect));
UIView *intersectView = [[UIView alloc] init];
intersectView.frame = rect;
intersectView.backgroundColor = [UIColor redColor];
[self.view addSubview:intersectView];
6、CGRectIsNull(<#CGRect rect#>)
是否為空
CG_EXTERN bool CGRectIsNull(CGRect rect)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
UIView *view1 = [[UIView alloc] init];
view1.frame = CGRectMake(100, 150, 100, 100);
view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view1];
CGRect intersectRect = CGRectIntersection(view.frame, view1.frame);
//0說明有交集
NSLog(@"%d",CGRectIsNull(intersectRect));
7、CGRectEqualToRect(<#CGRect rect1#>, <#CGRect rect2#>)
判斷兩個矩形是否相同,包括位置和形狀
CG_EXTERN bool CGRectEqualToRect(CGRect rect1, CGRect rect2)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
2016年12月21日更新
iOS中的座標轉換:
經常遇到的需求是把cell上的子檢視的座標轉為視窗的座標,我們需要的轉換如下:
[cell convertPoint:tmpImage.center toView:destinationView] 這裡的cell的位置不能傳入子檢視的座標,需要注意一下。