ios - Creating a custom slider using Sprite Kit - how do I pass a @selector? -
a sprite kit game i'm working on uses custom slider act color picker (the color picked slider track graphic, uiimage
containing gradient).
i looked using customised uislider
, standard ios ui controls don't play sprite kit's scenes: they're difficult position relative rest of scene (as exist subview of main view rather part of skscene
), pop existence (rather transitioning rest of scene) , must manually removed when exiting scene. in short, implementing them pain, , don't integrate seamlessly.
i've started implementing custom slider using skspritenode
s, building on graf's excellent skbutton class, , have slider track , handle in place. handle sliding left , right along track, , setting value between 0 , 1 (just uislider
). i've called class skslider
.
what pass skslider
@selector
, in same way uislider
, can define function inside skscene
slider execute:
[myslider addtarget:self action:@selector(slidervaluechanged:) forcontrolevents:uicontroleventvaluechanged];
the function i'm trying invoke when slider's changed looks this:
-(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); // rawdata contains image data in 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; }
(code found here, interested.)
but i'm not sure how should set in skslider
's interface. point me in right direction?
just implement missing method in custom slider. add in interface:
- (void)addtarget:(id)target action:(sel)action forcontrolevents:(uicontrolevents)controlevents;
and in implementation store target
, action
properties.
@property (nonatomic, strong) id target; @property (nonatomic) sel action; - (void)addtarget:(id)target action:(sel)action forcontrolevents:(uicontrolevents)controlevents { _target = target; _action = action; }
then call action
on target
when value changes:
objc_msgsend(_target, _action);
remember import:
#import <objc/message.h>
then updates slider create valuechanged
method , add it:
- (void)valuechanged { cgfloat value = _yourslider.value; // value. } [_yourslider addtarget:self action:@selector(valuechanged) forcontrolevents: uicontroleventvaluechanged];
Comments
Post a Comment