我正在开发的 Sprite Kit 游戏使用自定义滑块充当颜色选择器(颜色是从滑块轨道图形中选取的,这是一个UIImage
包含梯度)。
我研究过使用定制的UISlider
,但是标准的 IOS UI 控件不能很好地与 Sprite Kit 的场景配合使用:它们很难相对于场景的其余部分进行定位(因为它们作为主视图的子视图存在,而不是作为主视图的一部分)SKScene
),它们会突然出现(而不是与场景的其余部分一起过渡),并且在退出场景时必须手动删除。简而言之,实现它们很痛苦,而且它们不能无缝集成。
我已经开始使用实现自定义滑块SKSpriteNode
s,建立在Graf https://stackoverflow.com/users/2724767/graf's 优秀的SKButton类 https://stackoverflow.com/questions/19082202/spritekit-setting-up-buttons-in-skscene/19199748#19199748,并将滑块轨道和手柄安装到位。手柄沿着轨道左右滑动,并设置0到1之间的值(就像一个UISlider
)。我称这个类为SKSlider
.
我想做的是通过SKSlider
a @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 https://sites.google.com/site/gangeshwardev/ios-library/get-pixel-rgb-or-uicolor-data-from-a-uiimage-or-cgimage-on-cgpoint,有兴趣的人。)
但我不确定应该如何在SKSlider
的界面。有人能指出我正确的方向吗?