使用GameKit 发送复杂的数据,
通过GameKit以字符串的形式发送颜色信息,这里涉及到一个知识点就是序列化数据和反序列化数据
关于复杂数据的发送有两个方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 -(NSString *) stringFromColor { const CGFloat *c = CGColorGetComponents (self .CGColor); CGColorSpaceModel csm = CGColorSpaceGetModel (CGColorGetColorSpace (self .CGColor)); return (csm == kCGColorSpaceModelRGB) ? [NSString stringWithFormat:@"%0.2f %0.2f %0.2f %0.2f" , c[0 ], c[1 ], c[2 ], c[3 ]] : [NSString stringWithFormat:@"%0.2f %0.2f %0.2f %0.2f" , c[0 ], c[0 ], c[0 ], c[1 ]]; } +(UIColor *) colorWithString: (NSString *) colorString { const CGFloat c[4 ]; sscanf([colorString cStringUsingEncoding:NSUTF8StringEncoding ], "%f %f %f %f" , &c[0 ], &c[1 ], &c[2 ], &c[3 ]); return [UIColor colorWithRed:c[0 ] green:c[1 ] blue:c[2 ] alpha:c[3 ]]; }
这就是颜色转字符串,字符串转颜色的方法,这里实现支持ios5
这点代码放在了DrawView 文件中
整个发送复杂数据的流程如下
ComplexObjectsViewController.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #import <UIKit/UIKit.h> #import "GameKitHelper.h" #import "DrawView.h" #define BARBUTTON(TITLE, SELECTOR) [[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] #define COLOR_ARRAY [NSArray arrayWithObjects:[UIColor whiteColor], [UIColor lightGrayColor], [UIColor darkGrayColor], [UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor blueColor], [UIColor purpleColor], nil] #define BASE_TINT [UIColor darkGrayColor] #define DATAPATH [NSString stringWithFormat:@"%@/Documents/drawing.archive" , NSHomeDirectory()] @interface ComplexObjectsViewController : UIViewController -(void ) archiveInterface; @end
ComplexObjectsViewController.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 #import "ComplexObjectsViewController.h" @interface ComplexObjectsViewController ()@end @implementation ComplexObjectsViewController - (void )viewDidLoad { [super viewDidLoad]; self .view.backgroundColor = [UIColor blackColor]; self .navigationController.navigationBar.tintColor = BASE_TINT; [self unarchiveInterface]; NSMutableArray *items = [NSMutableArray array]; for (UIColor *color in COLOR_ARRAY) { [items addObject:[self swatchWithColor:color]]; } UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:items]; seg.tag = 102 ; seg.segmentedControlStyle = UISegmentedControlStyleBar ; seg.center = CGPointMake (160.0 f, 416.0 f - 15.0 f); seg.tintColor = BASE_TINT; seg.selectedSegmentIndex = 0 ; [seg addTarget:self action:@selector (colorChange:) forControlEvents:UIControlEventValueChanged ]; [self .view addSubview:seg]; self .navigationItem.leftBarButtonItem = BARBUTTON(@"清除" , @selector (doClear)); [GameKitHelper sharedInstance].dataDelegate = self .view; [GameKitHelper sharedInstance].sessionID = @"Drawing Together" ; [GameKitHelper assignViewController:self ]; } - (void )didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(UIImage *) swatchWithColor:(UIColor *)color { float side = 20.0 f; UIGraphicsBeginImageContext (CGSizeMake (side, side)); CGContextRef context = UIGraphicsGetCurrentContext (); [color setFill]; CGContextFillRect (context, CGRectMake (0.0 f, 0.0 f, side, side)); UIImage *img = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); return img; } -(void ) doClear { [(DrawView *)[self .view viewWithTag:101 ] clear]; } -(void ) colorChange:(UISegmentedControl *)seg { UIColor *color = [COLOR_ARRAY objectAtIndex:seg.selectedSegmentIndex]; DrawView *dv = (DrawView *)[self .view viewWithTag:101 ]; dv.currentColor = color; } -(void ) archiveInterface { DrawView *dv = (DrawView *)[self .view viewWithTag:101 ]; [NSKeyedArchiver archiveRootObject:dv toFile:DATAPATH]; } -(void ) unarchiveInterface { DrawView *dv = [NSKeyedUnarchiver unarchiveObjectWithFile:DATAPATH]; if (!dv) { dv = [[DrawView alloc] initWithFrame:CGRectMake (0.0 f, 0.0 f, 320.0 f, 416.0 f - 30.0 f)]; dv.userInteractionEnabled = YES ; dv.tag = 101 ; [self .view addSubview:dv]; } } @end
这里的GameKitHelper.h和GameKitHelper.m文件可以到这里下载GameKitHelper