您的位置:首页 > 移动开发 > IOS开发

iOS - OC NSPoint 位置

2016-08-17 17:45 344 查看

前言

结构体,这个结构体用来表示事物的一个坐标点。

typedef CGPoint NSPoint;

struct CGPoint {
CGFloat x;
CGFloat y;
};

typedef struct CGPoint CGPoint;


1、NSPoint 结构体变量的创建与调用

// NSPoint 结构体变量的创建与赋值

// 先定义变量,再赋值
NSPoint point1;
point1.x = 6;
point1.y = 1;

// 定义时直接赋值
NSPoint point2 = {7, 2};

// 给指定成员赋值
NSPoint point3 = {.y = 3, .x = 8};

// 使用函数赋值
NSPoint point4 = NSMakePoint(9, 4);

// 使用等价的结构体定义,等价于 CGPoint point5 = CGPointMake(10, 5);
NSPoint point5 = CGPointMake(10, 5);

// NSPoint 结构体变量值的调用

NSLog(@"point1: %.0f, %.0f", point1.x, point1.y);
NSLog(@"point2: %.0f, %.0f", point2.x, point2.y);
NSLog(@"point3: %.0f, %.0f", point3.x, point3.y);
NSLog(@"point4: %.0f, %.0f", point4.x, point4.y);
NSLog(@"point5: %.0f, %.0f", point5.x, point5.y);

2、NSPoint 与 NSString 的相互转换

// NSPoint 转 NSString
NSString *stringFronPoint = NSStringFromPoint(point5);

// NSString 转 NSPoint
NSPoint point6 = NSPointFromString(stringFronPoint);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: