Search code examples
ioscolor-pickertouchesmoved

Limit view movement to a circular area


I want to setup a custom color picker. I have setup a circular image with a picker bug. But, I want the bug to only move over the image (there are no colours outside the image), or, another way, only in the circle of the image size.

How can I limit the bug position?

enter image description here


Solution

  • I understand that you want to keep your picker inside the circle image. To do that just simply grab center point (p1) of your circle image and center point (p2) of currnet position of the picker. Calculate distans between both:

     float distance = sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
    

    And if the distance is more that circle radius stop move your picker:

    if(distance <= radius) 
            return YES;// Let the picker move, it's inside the circle image
    else
        return NO; // Picker outside the circle image
    

    Hope this is what you are about.