【发布时间】:2014-03-06 09:24:01
【问题描述】:
我正在开发的一个 Sprite Kit 游戏使用自定义滑块作为颜色选择器(颜色是从滑块轨迹图形中选择的,这是一个包含渐变的 UIImage)。
我研究过使用定制的UISlider,但标准的 IOS UI 控件不能很好地与 Sprite Kit 的场景配合使用:它们很难相对于场景的其余部分进行定位(因为它们作为主视图而不是作为SKScene 的一部分),它们会突然出现(而不是与场景的其余部分一起过渡),并且必须在退出场景时手动删除。简而言之,实施它们很痛苦,而且它们不能无缝集成。
我已经开始使用SKSpriteNodes 实现一个自定义滑块,以Graf 的excellent SKButton class 为基础,并设置了滑块轨道和手柄。手柄沿着轨道左右滑动,并设置一个介于 0 和 1 之间的值(就像UISlider)。我已将此类称为SKSlider。
我想做的是将SKSlider 传递给@selector,就像传递UISlider 一样,所以我可以在SKScene 中定义一个函数来执行滑块:
[mySlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
当滑块改变时我试图调用的函数如下所示:
-(UIColor*)getRGBAFromImage:(UIImage*)image atX:(float)xp atY:(float)yp
{
NSMutableArray *resultColor = [NSMutableArray array];
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * yp) + xp * bytesPerPixel;
CGFloat red = (rawData[byteIndex] * 1.0) /255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0)/255.0 ;
CGFloat blue = (rawData[byteIndex + 2] * 1.0)/255.0 ;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0) /255.0;
byteIndex += 4;
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
[resultColor addObject:color];
NSLog(@"width:%i hight:%i Color:%@",width,height,[color description]);
free(rawData);
return color;
}
(代码找到here,有兴趣的人。)
但我不确定应该如何在SKSlider 的界面中进行设置。有人能指出我正确的方向吗?
【问题讨论】:
标签: ios objective-c selector sprite-kit