我正在使用的Sprite Kit遊戲使用自定義滑塊來充當顏色選擇器(顏色從滑塊軌道圖形中挑選,這是一個包含漸變的UIImage
)。使用Sprite Kit創建自定義滑塊 - 如何傳遞一個@selector?
我使用自定義的UISlider
進行了研究,但標準IOS UI控件在Sprite Kit的場景中播放不好:它們很難相對於場景的其餘部分進行定位(因爲它們作爲主體的子視圖存在觀看而不是作爲SKScene
的一部分),它們會彈出存在(而不是與場景的其餘部分過渡),並且必須在退出場景時手動移除。簡而言之,實施它們是一種痛苦,而且它們不能無縫集成。
我已經開始使用SKSpriteNode
s實現自定義滑塊,建立在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
的界面中設置它。有人能指出我正確的方向嗎?
嗯,到了那裏我想......但它現在出現了錯誤:'將Objective-C指針隱式轉換爲'SEL'(又名'SEL *')不允許使用ARC並且'PerformSelector可能導致泄漏,因爲它的選擇器是未知的...... – MassivePenguin
我更新了我的答案。檢查出來:) – RaffAl
我仍然得到ARC限制警告說隱式轉換是不允許的:( – MassivePenguin