1
转自:http://hi.baidu.com/myyuchong/item/51972681a54f74c2b1715454
把CGPoint放入数组的过程是这样的:
NSArray *array=[NSArray arrayWithObjects:NSStringFromCGPoint(0.0f,0.0f),NSStringFromCGPoint(0.0f,0.0f),nil];
从数组中取值的过程是这样的:
CGPoint point=CGPointFromNSString([array objectAtIndex:2]);
2
转自:http://blog.sina.com.cn/s/blog_65a8ab5d0101c0n2.html
因为CGRect,CGPoint等不是NSObject的子类,因此不能够直接添加到NSMutableArray中,所以要先将其转换成NSObject的子类。NSValue类可以解决这个问题,见NSValue类的声明:
@interface NSValue (NSValueUIGeometryExtensi
+ (NSValue *)valueWithCGPoint:(CGPoint)point;
+ (NSValue *)valueWithCGSize:(CGSize)size;
+ (NSValue *)valueWithCGRect:(CGRect)rect;
+ (NSValue*)valueWithCGAffineTransfo
+ (NSValue *)valueWithUIEdgeInsets:(UIEdgeInsets)insets;
+ (NSValue *)valueWithUIOffset:(UIOffset)insets__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
- (CGPoint)CGPointValue; - (CGSize)CGSizeValue;
- (CGRect)CGRectValue;
- (CGAffineTransform)CGAffineTransformValue;
- (UIEdgeInsets)UIEdgeInsetsValue;
- (UIOffset)UIOffsetValue__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
@end
如下,可以将CGRect,CGPoint等的数值转换成NSValue类的数值。
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lockButton.png"]];
imageView.frame = CGRectMake(10 * (l + 1) +imageView.frame.size.width * l, 120 + 50 * r,imageView.frame.size.width, imageView.frame.size.height);
[self.lockImageRectArray addObject:[NSValuevalueWithCGRect:imageView.frame]];
[self addSubview:imageView];
通过下面的代码,又可以将NSValue转换成CGRect,CGPoint等类型的数值。
CGRect imageRect = [[self.lockImageRectArray objectAtIndex:l]CGRectValue];
通过NSValue就可以实现CGxxx类型的数值存储在NSMutable,NSDictionary类型的数组中。
3
转自:http://www.cocoachina.com/bbs/read.php?tid=3768&page=1
// 存:
[array addObject:[
NSValue
valueWithCGPoint:point]];
// 取:
CGPoint anotherPoint = [[array objectAtIndex:index] CGPointValue];
// 另外:
xCoords = [
NSArray
arrayWithObjects:[
NSNumber
numberWithFloat:30.00],
[
NSNumber
numberWithFloat:60.00],
nil
];
yCoords = [
NSArray
arrayWithObjects:[
NSNumber
numberWithFloat:30.00],
[
NSNumber
numberWithFloat:60.00],
nil
];
for
(
int
cnt=0; cnt<[xCoords count]; cnt++){
NSNumber
*xNum = [xCoord objectAtIndex:cnt];
NSNumber
*yNum = [yCoord objectAtIndex:cnt];
CGPoint point = CGPointMake(xNum.floatValue, yNum.floatValue);
}
转自:http://www.cocoachina.com/bbs/read.php?tid=3768&page=2
4
CGPoint point,
存: [array addObject:NSStringFromCGPoint(point)];
取:CGPoint point1 = CGPointFromString([array objectAtIndex:index]);
5
With UIKit Apple added support for CGPoint to NSValue, so you can do:
NSArray *points = [NSArray arrayWithObjects:
[NSValue valueWithCGPoint:CGPointMake(5.5, 6.6)],
[NSValue valueWithCGPoint:CGPointMake(7.7, 8.8)],
nil];
List as many [NSValue] instances as you have CGPoint, and end the list in nil. All objects in this structure are auto-released.
On the flip side, when you're pulling the values out of the array:
NSValue *val = [points objectAtIndex:0];
CGPoint p = [val CGPointValue];
6
可以先转成NSCoder
-encodePoint:(CGPoint)point