ios - Adding an indicator at the current touch position? -


i developing color selector , using circular gradient , pangesture pick color.

can add crosshair or else current touch position?

i need crosshair visible should not interfere gradient.

this code adds gradient :

- (void)viewdidload {     [super viewdidload];          cgsize size = cgsizemake(self.view.bounds.size.width, ((self.view.bounds.size.height)/2));     uigraphicsbeginimagecontextwithoptions(cgsizemake(size.width, size.height), yes, 0.0);     [[uicolor whitecolor] setfill];     uirectfill(cgrectmake(0, 0,size.width,size.height));       int sectors = 180;     float radius = min(size.width, size.height)/2;     float angle = 2 * m_pi/sectors;     uibezierpath *bezierpath;     ( int = 0; < sectors; i++)     {         cgpoint center = cgpointmake(((size.width)/2), ((size.height)/2));         bezierpath = [uibezierpath bezierpathwitharccenter:center radius:radius startangle:i * angle endangle:(i + 1) * angle clockwise:yes];         [bezierpath addlinetopoint:center];         [bezierpath closepath];         uicolor *color = [uicolor colorwithhue:((float)i)/sectors saturation:1. brightness:1. alpha:1];         [color setfill];         [color setstroke];         [bezierpath fill];         [bezierpath stroke];     }     img = uigraphicsgetimagefromcurrentimagecontext();     gradientview = [[uiimageview alloc]initwithimage:img];;     uipangesturerecognizer *pangesture = [[uipangesturerecognizer alloc]initwithtarget:self action:@selector(handlepan:)];      colorview = [[uiview alloc] init];     colorview.frame = cgrectmake(0, 00, 50, 50);       rtext.textalignment = nstextalignmentcenter;     gtext.textalignment = nstextalignmentcenter;     btext.textalignment = nstextalignmentcenter;     saturationtext.textalignment = nstextalignmentcenter;     alphatext.textalignment = nstextalignmentcenter;     brightnesstext.textalignment = nstextalignmentcenter;      rtext.keyboardtype = uikeyboardtypenumberpad;     gtext.keyboardtype = uikeyboardtypenumberpad;     btext.keyboardtype = uikeyboardtypenumberpad;      saturationtext.keyboardtype = uikeyboardtypenumberpad;     alphatext.keyboardtype = uikeyboardtypenumberpad;     brightnesstext.keyboardtype = uikeyboardtypenumberpad;        gradientview.frame = cgrectmake(0,0,size.width,size.height);     self.view.backgroundcolor = [uicolor whitecolor];     [self.view addsubview:gradientview];     gradientview.userinteractionenabled = yes;     [gradientview addgesturerecognizer: pangesture];      [self.view addsubview:colorview];      } 

the handlepan method :

    - (ibaction)handlepan:(uipangesturerecognizer *)sender {       if (sender.numberoftouches)         {             cgsize size = cgsizemake(self.view.bounds.size.width, (self.view.bounds.size.height)/2);              float radius = min(size.width, size.height)/2;              [alphaslider addtarget:self action:@selector(changeopacity:) forcontrolevents:uicontroleventvaluechanged];             [hellslider addtarget:self action:@selector(changebrightness:) forcontrolevents:uicontroleventvaluechanged];             [saturationslider addtarget:self action:@selector(saturate:) forcontrolevents:uicontroleventvaluechanged];             [rslider addtarget:self action:@selector(redslider:) forcontrolevents:uicontroleventvaluechanged];             [gslider addtarget:self action:@selector(greenslider:) forcontrolevents:uicontroleventvaluechanged];             [bslider addtarget:self action:@selector(blueslider:) forcontrolevents:uicontroleventvaluechanged];                 cgpoint lastpoint = [sender locationoftouch: sender.numberoftouches - 1 inview: gradientview];             cgpoint center = cgpointmake((size.width/2), (size.height /2));             cgpoint delta = cgpointmake(lastpoint.x - center.x,  lastpoint.y - center.y);             cgfloat angle = (delta.y == 0 ? delta.x >= 0 ? 0 : m_pi : atan2(delta.y, delta.x));             angle = fmod(angle,  m_pi * 2.0);             angle += angle >= 0 ? 0 : m_pi * 2.0;             if((lastpoint.x - center.x) + (lastpoint.y - center.y)/2 < radius)             {                 uicolor *color = [uicolor colorwithhue: angle / (m_pi * 2.0) saturation:saturationslider.value brightness:hellslider.value alpha:alphaslider.value];                 if ([color getred: &r green: &g blue:&b alpha: &a])                 {                     nslog(@"color value - r : %g g : %g : b %g", r*255, g*255, b*255);                 }                 float red = r;                 float green = g;                 float blue = b;                 rtext.text = [nsstring stringwithformat:@"%.0f",rslider.value];                 gtext.text = [nsstring stringwithformat:@"%.0f",green*255];                 btext.text = [nsstring stringwithformat:@"%.0f",blue*255];                   colorview.backgroundcolor = [uicolor colorwithred:(rtext.text.floatvalue/255) green:(gtext.text.floatvalue/255) blue:(btext.text.floatvalue/255) alpha:alphaslider.value];                 rslider.value = red*255;                 gslider.value = green*255;                 bslider.value = blue*255;                 alphatext.text = [nsstring stringwithformat:@"%.2f",alphaslider.value];                 brightnesstext.text = [nsstring stringwithformat:@"%.2f",hellslider.value];                 saturationtext.text = [nsstring stringwithformat:@"%.2f",saturationslider.value];              }              } } 

question solved, thank you.

well, 'can' add crosshair @ current touch position.

i can tell 1 of many possible ways of doing this.

implement

 - (ibaction)handlepan:(uipangesturerecognizer *)recognizer 

action handler.

load image of crosshair uiimage (say overlayimage):

  overlayimage =[uiimage imagenamed:@"crosshair.png"]; 

and set uiview (say overlayview). [this can done in viewdidload method itself.]

now, add following code handlepan method:

cgpoint translation = [recognizer translationinview:colorview]; //whichever view want. _overlayview.center = cgpointmake(_overlayview.center.x + translation.x,                                      _overlayview.center.y + translation.y); [recognizer settranslation:cgpointmake(0, 0) inview:colorview];   if (recognizer.state == uigesturerecognizerstateended) {      cgpoint velocity = [recognizer velocityinview:colorview];     cgfloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));     cgfloat slidemult = magnitude / 800;    // nslog(@"magnitude: %f, slidemult: %f", magnitude, slidemult);      float slidefactor = 0.1 * slidemult; // increase more of slide     cgpoint finalpoint = cgpointmake(_overlayview.center.x + (velocity.x * slidefactor),                                      _overlayview.center.y + (velocity.y * slidefactor));     finalpoint.x = min(max(finalpoint.x, 0), colorview.bounds.size.width);     finalpoint.y = min(max(finalpoint.y, 0), colorview.bounds.size.height);      [uiview animatewithduration:slidefactor*2 delay:0 options:uiviewanimationoptioncurveeaseout animations:^{         _overlayview.center = finalpoint;     } completion:nil]; 

that should trick.

you may try doing implementing touchesbegan , touchesmoved method.


Comments