i using imageview
render circular gradient selecting color.
the problem is, long pangesturerecognizer
remains inside rectangular imageview
, continue returning colors when outside of circular gradient.
is there way enforce circular bounds?
here code adding gradient imageview
:
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];;
you should check if touch inside of imaginary circle.
all need pretty basic trigonometry. need calculate distance between touch point , center of view. if distance bigger radius of colorview want return without changing new color (or set value @ edge of colorview)
i use hue , saturation picker.
- (void)gesturerecognizerdidpan:(uipangesturerecognizer *)gesture { cgfloat saturation = 0.0f; cgfloat hue = 0.0f; uiview *colorview = gesture.view; cgpoint location = [gesture locationinview:colorview]; // assume colored area circle inside of square rect cgpoint colorviewcenter = cgpointmake(ceilf(cgrectgetwidth(colorview.bounds)/2.0f), ceilf(cgrectgetheight(colorview.bounds)/2.0f)); cgfloat colorviewradius = floorf(cgrectgetwidth(colorview.bounds)/2.0f); cgfloat dx = location.x - colorviewcenter.x; cgfloat dy = location.y - colorviewcenter.y; cgfloat touchradius = sqrtf(powf(dx, 2)+powf(dy, 2)); if (touchradius > colorviewradius) { // touch outside of circular area // set saturation max saturation = 1.0f; // or: // return; } else { saturation = touchradius / colorviewradius; } cgfloat anglerad = atan2f(dx, dy); cgfloat angledeg = (anglerad * (180.0f/m_pi) - 90); if (angledeg < 0.f) { angledeg += 360.f; } hue = angledeg / 360.0f; // tell target }
Comments
Post a Comment