本帖最后由 paradise6 于 2015-7-24 13:55 编辑
1. NSRange
创建
NSMakeRange(location, length); //location和length为unsigned long
使用
查找字符串@"love"在NSString *str = @"i love oc";中的范围
NSRange r1 = [str rangeOfString:@"love"];
NSLog(@"loc = %ld, length = %ld", r1.location, r1.length);
2. NSPoint/CGPoint
创建
NSMakePoint(x, y);
CGPointMake(x, y); // x和y为double类型
使用
CGPoint p1 = CGPointMake(0, 0); //坐标原点,等同于
CGPointZero;
3. NSSize/CGSize
创建
NSMakeSize(width, height);
CGSizeMake(width, height); //width和height为double类型
使用
NSSize s1 = CGSizeMake(100, 50);
4. NSRect/CGRect
创建
NSMakeRect(NSPoint origin, NSSize size);
CGRectMake(CGPoint origin, CGSize size);
使用
CGRect r1 = CGRectMake(0, 0, 100, 50);
CGRect r2 = {{0, 0}, {100, 90}};
CGRect r3 = {p1, s1};
CGRect r4 = {CGPointZero, CGSizeMake(100, 50)};
CGRectZero = {CGPointZero, CGSizeZero};
访问内部成员变量
NSLog(@"x=%f, y=%f, width=%f, height=%f", r3.origin.x, r3.origin.y, r3.size.width, r3.size.height);
5. 结构体的应用
比较二者是否相同
BOOL CGPointEqualToPoint(CGPoint point1, CGPoint point2);
BOOL CGSizeEqualToSize(CGSize size1, CGSize size2);
BOOL CGRectEqualToRect(CGRect rect1, CGRect rect2);
举例:
CGPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10));
判断一块区域是否包含某个点
CGRectContainsPoint(CGRect rect, CGPoint point);
举例:
BOOL b1 = CGRectContainsPoint(CGRectMake(50, 40, 100, 50),
CGPointMake(130, 70));
//在手机屏幕上左上角为坐标原点,计算得知b1=YES;
将结构体转为字符串
NSString *str1 = NSStringFromPoint(p1); //{x, y}
NSString *str2 = NSStringFromSize(s1); //{width, length}
NSString *str3 = NSStringFromRect(r1); //{x, y}, {width, length}
注意:
以上结构体对应的函数声明在Foundation,定义在CoreGraphics
如使用,需添加CoreGraphics框架 |
|