NSValue包装对象指针,CGRect结构体等

@interface NSValue (NSValueUIGeometryExtensions)

+ (NSValue *)valueWithPointer:(const void *)pointer;//保存对象指针

+ (NSValue *)valueWithCGPoint:(CGPoint)point;//保存CGPoint结构体
+ (NSValue *)valueWithCGSize:(CGSize)size;//保存CGSize结构体
+ (NSValue *)valueWithCGRect:(CGRect)rect;//保存CGRect结构体
+ (NSValue *)valueWithCGAffineTransform:(CGAffineTransform)transform;
+ (NSValue *)valueWithUIEdgeInsets:(UIEdgeInsets)insets;

- (void *)pointerValue;
- (CGPoint)CGPointValue;
- (CGSize)CGSizeValue;
- (CGRect)CGRectValue;
- (CGAffineTransform)CGAffineTransformValue;
- (UIEdgeInsets)UIEdgeInsetsValue;

@end

 

偶就抽三个来写吧。。其它的方式差不多。。。

//
// NSArrayAndSelectorViewController.m
// NSArrayAndSelector
//
// Created by Lixf on 09-10-15.
// Copyright Lixf 2009. All rights reserved.
//

#import "NSArrayAndSelectorViewController.h"

@implementation NSArrayAndSelectorViewController
@synthesize SelectorArray, RectArray, TransformArray;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];

//NSValue包装指针
NSValue* start = [NSValue valueWithPointer:@selector(start:)];
NSValue* help = [NSValue valueWithPointer:@selector(help:)];

//NSArray 添加NSValue包装后的指针
NSArray *Array = [[NSArray alloc] initWithObjects:start, help, nil];
self.SelectorArray = Array;
[Array release];


CGRect rect1 = {10, 10, 200, 200};
CGRect rect2 = {20, 20, 100, 100};

//NSValue包装CGRect结构
NSValue *aRect1 = [NSValue valueWithCGRect:rect1];
NSValue *aRect2 = [NSValue valueWithCGRect:rect2];
//NSArray 添加NSValue包装后的指针
NSArray *reArray = [[NSArray alloc] initWithObjects:aRect1, aRect2, nil];
self.RectArray = reArray;
[reArray release];

//CGAffineTransform
CGAffineTransform transform1 = CGAffineTransformMakeRotation(40);
CGAffineTransform transform2 = CGAffineTransformMakeRotation(-40);
NSValue *aTransform1 = [NSValue valueWithCGAffineTransform:transform1];
NSValue *aTransform2 = [NSValue valueWithCGAffineTransform:transform2];
NSArray *TransArray = [[NSArray alloc] initWithObjects:aTransform1, aTransform2, nil];
self.TransformArray = TransArray;
[TransArray release];
[testView setFrame:[[RectArray objectAtIndex:0] CGRectValue]];
[testView setTransform:[[TransformArray objectAtIndex:0] CGAffineTransformValue]];
[self performSelector:[[SelectorArray objectAtIndex:0] pointerValue] withObject:nil afterDelay:0.5];
}

-(void)start:(id)sender{
NSLog(@"start");
[testView setFrame:[[RectArray objectAtIndex:1] CGRectValue]];
[testView setTransform:[[TransformArray objectAtIndex:1] CGAffineTransformValue]];
[self performSelector:[[SelectorArray objectAtIndex:1] pointerValue] withObject:nil afterDelay:0.5];
}

-(void)help:(id)sender{
NSLog(@"help");
//[testView setFrame:[[RectArray objectAtIndex:0] CGRectValue]];
//[testView setTransform:[[TransformArray objectAtIndex:0] CGAffineTransformValue]];
}


附件代码(重命名rar):

你可能感兴趣的:(value)