ios CGRect
/*
rect(x,y,width,height);
width, height正负代表了从原点的绘制方向,矩形的长宽都是取得绝对值
*/
// Do any additional setup after loading the view, typically from a nib.
#pragma mark -------------------------------------------生成CGRect---------------------------------------
CGRect originalRect = CGRectMake(80.44, 30.66, 100.77, 95.88);
NSLog(@"originalRect = %@",NSStringFromCGRect(originalRect));
// CGRectInfinite 用法
/*
生成无穷大矩形
*/
NSLog(@"infiniteRect = %@",NSStringFromCGRect(CGRectInfinite));
// CGRectNull 用法
NSLog(@"nullRect = %@",NSStringFromCGRect(CGRectNull));
//CGRectZero 用法
/*
CGRectMake(0, 0, 0, 0)
*/
NSLog(@"zeroRect = %@",NSStringFromCGRect(CGRectZero));
// CGRectInset 用法
CGRect insetRect = CGRectInset(originalRect, 1, 2);
/*
CGRectInset(originalRect, dx, dy);
insetRect.origin.x = originalRect.origin.x + dx ;
insetRect.origin.y = originalRect.origin.y + dy;
insetRect.size.width = originalRect.size.width - 2 * dx;
insetRect.size.height = originalRect.size.height - 2 * dy;
*/
NSLog(@"insetRect = %@",NSStringFromCGRect(insetRect));
//CGRectOffset 用法
CGRect offsetRect = CGRectOffset(originalRect, -1, -2);
/*
CGRectOffset(originalRect, dx, dy);
insetRect.origin.x = originalRect.origin.x + dx ;
insetRect.origin.y = originalRect.origin.y + dy;
insetRect.size.width = originalRect.size.width;
insetRect.size.height = originalRect.size.height;
*/
NSLog(@"offsetRect = %@",NSStringFromCGRect(offsetRect));
//CGRectIntegral 用法
/*
将origin值向下调整到最近整数,size向上调整到最近整数,使生成的CGRect可以完全包含原来的CGRect.
*/
CGRect integralRect = CGRectIntegral(originalRect);
NSLog(@"integralRect = %@",NSStringFromCGRect(integralRect));
//CGRectStandardize 用法
/*
CGRectStandardize(testRect)
standardizeRect.size.width = fabsf(testRect.size.width)//testRect.size.width 的绝对值
standardizeRect.size.height = fabsf(testRect.size.height)//testRect.size.height 的绝对值
*/
CGRect testRect = CGRectMake(-5.5, -6.6, -7.7, -8.8);
CGRect standardizeRect = CGRectStandardize(testRect);
NSLog(@"standardizeRect = %@",NSStringFromCGRect(standardizeRect));
//CGRectUnion 用法
/*
CGRectUnion(rect1,rect2) 返回值是能够包含两个矩形的最小矩形
unionResultRect.origin.x = min(rect1.origin.x, rect2.origin.x);
unionResultRect.origin.y = min(rect1.origin.y, rect2.origin.y);
unionResultRect.size.width = min(rect1.origin.y, rect2.origin.y);
unionResultRect.size.height = min(rect1.origin.y, rect2.origin.y);
*/
CGRect unionRect1 = CGRectMake(100, 50, 60, 70);
CGRect unionRect2 = CGRectMake(180, 20, 30, 40);
CGRect unionRect3 = CGRectMake(120, 80, 80, 90);
CGRect unionResultRect1 = CGRectUnion(unionRect1, unionRect2);
CGRect unionResultRect2 = CGRectUnion(unionRect1, unionRect3);
NSLog(@"unionResultRect1 = %@, unionResultRect2 = %@",NSStringFromCGRect(unionResultRect1),NSStringFromCGRect(unionResultRect2));
//CGRectIntersection 用法
/*
CGRectIntersection(rect1, rect2)
求两个矩形的交集,返回结果是两个矩形相交的区域,如果没有交集的话,返回值是NSNullRect
*/
CGRect interRect1 = CGRectMake(100, 50, 60, 70);
CGRect interRect2 = CGRectMake(180, 20, 30, 40);
CGRect interRect3 = CGRectMake(120, 80, 80, 90);
CGRect intersectionRect1 = CGRectIntersection(interRect1, interRect2);
CGRect intersectionRect2 = CGRectIntersection(interRect1, interRect3);
NSLog(@"intersectionRect1 = %@, intersectionRect2 = %@",NSStringFromCGRect(intersectionRect1),NSStringFromCGRect(intersectionRect2));
#pragma mark ------------------------------------------获取x,y,width,height---------------------------------------------------
//CGRectGetMinX(originalRect),CGRectGetMidX(originalRect),CGRectGetMaxX(originalRect),CGRectGetMinY(originalRect),CGRectGetMidY(originalRect),CGRectGetMaxY(originalRect) , CGRectGetWidth(originalRect), CGRectGetHeight(originalRect)用法
/*
minX = originalRect.origin.x;
midX = (minX + maxX)/2;
maxX = (originalRect.origin.x + originalRect.size.width);
minY = originalRect.origin.y;
midY = (minY + maxY)/2;
maxY = (originalRect.origin.y + originalRect.size.height);
width = originalRect.size.width;
height = originalRect.size.height;
*/
NSLog(@"minX = %f, midX = %f, maxX = %f, minY = %f, midY = %f, maxY = %f, width = %f, height = %f",CGRectGetMinX(originalRect),CGRectGetMidX(originalRect),CGRectGetMaxX(originalRect),CGRectGetMinY(originalRect),CGRectGetMidY(originalRect),CGRectGetMaxY(originalRect),CGRectGetWidth(originalRect), CGRectGetHeight(originalRect));
//CGRectGetWidth(originalRect), CGRectGetHeight(originalRect),用法
/*
width = originalRect.size.width;
height = originalRect.size.height;
*/
NSLog(@"width = %f, height = %f",CGRectGetWidth(originalRect), CGRectGetHeight(originalRect));
#pragma mark -----------------------------------------NSString 与 CGRect 转换-----------------------------------------------
//NSString 与 CGRect 转换
NSString *originalRectStr = NSStringFromCGRect(originalRect);
CGRect rectFromStr = CGRectFromString(originalRectStr);
NSLog(@"originalRectStr = %@, rect = %@",originalRectStr,NSStringFromCGRect(rectFromStr));
#pragma mark -----------------------------------------CGRect 与 NSDictionary 转换---------------------------------------------
//.CGRect与NSDictionary转换
CFDictionaryRef frameDictRef = CGRectCreateDictionaryRepresentation(originalRect);
NSDictionary *frameDict = [NSDictionary dictionaryWithDictionary:(NSDictionary*)CFBridgingRelease(frameDictRef)]; // autoreleased
CGRect rectFromDic;
CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)frameDict, &rectFromDic);
NSLog(@"frameDict = %@, rectFromDic = %@",frameDict, NSStringFromCGRect(rectFromDic));
#pragma mark -----------------------------------------CGRect 与 NSValue 转换---------------------------------------------
//. CGRect与NSValue转换
/*
在NSArray,NSDictionary容器中只能存储Objetive-C对象,不能直接存储C结构体,所有需要存储矩形时,需要将其转换为NSValue值。
*/
NSValue *originalRectValue = [NSValue valueWithCGRect:originalRect];
CGRect valueRect = [originalRectValue CGRectValue];
NSLog(@"originalRectValue = %@, valueRect = %@",originalRectValue, NSStringFromCGRect(valueRect));
#pragma mark -----------------------------------------判断---------------------------------------------
//CGRectEqualToRect
/*
CGRectEqualToRect(CGRect rect1, CGRect rect2)
rect1 与 rect2 的origin,size 完全相同 ,返回YES;否则,返回NO;
*/
if (CGRectEqualToRect(unionRect1, unionRect2)) {
NSLog(@"相同");
} else {
NSLog(@"不相同");
}
//CGRectIsEmpty(CGRect rect)
/*
rect的宽度和高度只要有一个就会被定为emptyRect
*/
CGRect emptyRect = CGRectMake(0, 10, 0, 20);
if (CGRectIsEmpty(emptyRect)) {
NSLog(@"是 emptyRect");
} else {
NSLog(@"不是 emptyRect");
}
//CGRectIsNull(CGRect rect)
/*
rect 是NSNullRect 返回true
*/
if (CGRectIsNull(originalRect)) {
NSLog(@"是 NSNullRect ");
} else {
NSLog(@"不是 NSNullRect ");
}
//CGRectIsInfinite(CGRect rect)
/*
判断是不是无穷大矩形
*/
if (CGRectIsInfinite(originalRect)) {
NSLog(@"是 无穷大 ");
} else {
NSLog(@"不是 无穷大 ");
}
//CGRectContainsPoint(CGRect rect, CGPoint point)
/*
判断rect 是不是包含 point 这个点
*/
CGPoint position = CGPointMake(100, 100);
if (CGRectContainsPoint(originalRect, position)) {
NSLog(@" rect 包含 point");
} else {
NSLog(@" rect 不包含 point");
}
//CGRectContainsRect(CGRect rect1, CGRect rect2)
/*
判断 rect2 是不是在 rect1 里面
*/
if (CGRectContainsRect(unionRect1, unionRect2)) {
NSLog(@" rect1 包含 rect2");
} else {
NSLog(@" rect1 不包含 rect2");
}
//CGRectIntersectsRect(CGRect rect1, CGRect rect2)
/*
判断 rect1 与 rect2 是不是有交集
*/
if (CGRectIntersectsRect(unionRect1, unionRect2)) {
NSLog(@" rect1 与 rect2 有交集");
} else {
NSLog(@" rect1 与 rect2 没有交集");
}
ios CGRect的更多相关文章
- [转]IOS开发中的CGFloat、CGPoint、CGSize和CGRect
http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGGeometry/Reference ...
- IOS开发中的CGFloat、CGPoint、CGSize和CGRect
IOS开发中的CGFloat.CGPoint.CGSize和CGRect http://developer.apple.com/library/ios/#documentation/GraphicsI ...
- UI基础:UIView(window,frame,UIColor,CGPoint,alpha,CGRect等) 分类: iOS学习-UI 2015-06-30 20:01 119人阅读 评论(0) 收藏
UIView 视图类,视图都是UIView或者UIView子类 UIWindow 窗口类,用于展示视图,视图一定要添加window才能显示 注意:一般来说,一个应用只有一个window 创建一个UIW ...
- iOS 开发中的CGFloat,CGPoint,CGSize和CGRect
CGGeometry类定义几何元素的结构和操作集合元素的函数 1. 数据类型 CGFloat: 浮点值的基本类型 CGPoint: 表示一个二维坐标系中的点 CGSize: 表示一个矩形的宽度和高度 ...
- iOS 保存CGRect,CGPoint到NSArray'的方法
由于CGRect和CGPoint等对象是Struct,即结构体,不是继承于NSObject的,所以需要先用NSValue的方法,把他们转化成NSValue对象,之后就可以存入NSArray了! @in ...
- IOS常用CGRect的交错,边缘,中心的检测
转自:http://tsyouaschen.iteye.com/blog/1946957 判断给定的点是否被一个CGRect包含,可以用CGRectContainsPoint函数 BOOL conta ...
- iOS开发-UIView扩展CGRect
关于UIView的位置都会遇到,一般需要改变UIView的位置,需要先获取原有的frame位置,然后在frame上面修改,有的时候如果只是改变了一下垂直方向的位置,宽度和高度的一种,这种写法很麻烦.下 ...
- ios开发之--CGRect/CGSize/CGPoint/CGVector/CGAffineTransform/UIEdgeInsets/UIOffset和NSString之间的转换
仅做记录,一个函数和字符串之间的互相转换 方法如下: UIKIT_EXTERN NSString *NSStringFromCGPoint(CGPoint point); UIKIT_EXTERN N ...
- iOS开发小技巧--UIButton的另一种布局方法(第一种在layoutSubViews方法中,这一种利用苹果提供的两个返回CGRect的方法)
随机推荐
- MATLAB寻找数组前k个大值
有时候我们需要寻找数组的前k个大值并按照顺序输出, 在C语言可以通过快速排序等算法,快速求得,这里用matlab写了一个比较简单实用的程序(适用于数组长度不是特别大的情况). function [va ...
- 14.Xcode8imageview图片圆角不显示的bug
第一种方法: 在imageView调用clipToBounds的之前,[super awakeFromNib]之后,加上[self.contentView layoutIfNeeded];就可以了,反 ...
- C语言小练习四
题目要求:输入N个数,输出该数列中第k个最大者. 程序源码: #include <stdio.h> #include <string.h> void printResult(i ...
- javascript code snippet -- Forwarding Mouse Events Through Layers
Anyone who has worked with web apps has likely created a masking element at some point, and the grea ...
- spring mvc 使用ehcache
一.需要导入的jar包 1.ehcache.jar 2.ehcache-spring-annotations-1.2.0.jar 3.guava-r09.jar 4.slf4j-api-1.6.6.j ...
- 线程池ExecutorService
说到java开发,免不了跟多线程打交道.Executor框架便是Java 5中引入的,其内部使用了线程池机制,它在java.util.cocurrent 包下,通过该框架来控制线程的启动.执行和关闭, ...
- 视图合并、hash join连接列数据分布不均匀引发的惨案
表大小 SQL> select count(*) from agent.TB_AGENT_INFO; COUNT(*) ---------- 1751 SQL> select count( ...
- Smarty缓存的5个知识点
(1)页面缓存:整个页面全局的缓存 需要4个步骤: ①开启缓存 $smarty->caching = true; ②设置缓存的生命周期 $smarty->cache_lifetime ...
- VPN帐号(每1小时自动断开及更新密码)
免费的vpn帐号 http://freevpn.wwdhz.com/
- Codeforces #380 div2 C(729C) Road to Cinema
C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...