结构体
NSRange:
用来表示范围
创建
NSRange r1 = {2, 4}
NSRange r2 = {.location = 2, .length = 4}
NSRange r3 = NSMakeRange(2, 4)
查找某个字符串在str中的范围
NSString *str = @"i love oc";
NSRange range = [str rangeOfString:@"java"]
NSLog(@"location = %ld, length=%ld", range.location, range.length)
如果找不到,length=0,location=NSNotFound==-1
NSPoint:
用来表示一个点的坐标
创建
CGPoint p1 = NSMakePoint(10, 10);
NSPoint p2 = CGPointMake(20, 20);
表示原点
CGPointZero == CGPointMake(0, 0)
NSSize:
表示二维平面的尺寸
NSSize s1 = CGSizeMake(100, 50);
NSSize s2 = NSMakeSize(100, 50);
CGSize s3 = NSMakeSize(200, 60);
CGRect:
表示二维平面具体的一个尺寸和位置
CGRect r1 = CGRectMake(0, 0, 100, 50);
CGRect r2 = { {0, 0}, {100, 90}};
CGRect r3 = {p1, s2};
定义CGRect的另外方法
CGRect myRect(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
CGRect rect;
rect.origin.x = x;
rect.origin.y = y;
rect.size.width = width;
rect.size.height = height;
}
将结构体转为字符串
NSString *str = NSStringFromPoint(p1);
NSString *str = NSStringFromSize(s3);
NSString *str = NSStringFromRect(r1);
比较两个点是否相同(x、y)
BOOL b = CGPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10))
判断一个点是否在某个区域
BOOL b2 = CGRectContainsPoint(CGRectMake(50, 40, 100, 50), CGPointMake(60, 45))
使用这些CGPointEqualToPoint、CGRectContainsPoint等函数的前提是添加CoreGraphics框架