0

The color palette is a view added to main storyboard. I have used corner radius to make a circle. The small circle inside the palette is a subview created inside the palette view. The small circle is draggable. The problem is I can drag the small circle outside the main circle (palette).

How do I stop the small circle from being dragged once it reaches the border of the main circle (palette).

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

Calculate the distance between the centre point of the palette and the centre point of the picker circle (How to find the distance between two CG points?)

Add on the radius of the picker circle and if that exceeds the radius of the larger circle then you want to stop the dragging.

UPDATE:

enter image description here

So if the Distance + R2 is >= Radius 1 you've reached the edge of the circle and need to stop them dragging

UPDATE2

Based on the sample project you uploaded here is the correct code...

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches){


        CGPoint newPoint = [touch locationInView:self];
        newPoint.x -= startPoint.x;
        newPoint.y -= startPoint.y;
        CGRect frm = [picker frame];
        frm.origin = newPoint;

        CGFloat xDist = abs((newPoint.x + 15) - (self.center.x - self.frame.origin.x));
        CGFloat yDist = abs((newPoint.y + 15) - (self.center.y - self.frame.origin.y));

        CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));

        if ((distance + 15) >= (self.frame.size.width /2)) {
            // EDGE REACHED SO DON'T UPDATE
        } else {
            [picker setFrame:frm];
        }

    }
}

You only want to update the frame if its still in bounds and your distance calculations weren't taking into account the offset of the containing view

HTH

Community
  • 1
  • 1
Flexicoder
  • 8,251
  • 4
  • 42
  • 56
  • I don't understand the second part – Divine Propotion May 18 '14 at 16:18
  • @DivinePropotion I've added a bit more with your drawn updated – Flexicoder May 18 '14 at 16:30
  • I am still having a hard time achieving this. Could you please review my code and let me know what I am doing wrong. This code is placed inside paletteView class. http://paste.ubuntu.com/7494466/ – Divine Propotion May 20 '14 at 19:54
  • @DivinePropotion had a quick look, couple of things, use the `center` properties of the views rather than calculate it. you've stated that your picker is 30 x 30 then the radius should be 15 not 18.75? Palette radius should be 137.5. Also put some `NSLog` in to see that your points match up, e.g. if the picker is in the center of the palette are their center points the same? Is pickerVIew a sub view of the palette? You might need to adjust the picker center by the origin of the palette? – Flexicoder May 21 '14 at 08:59
  • What do you mean by adjust the picker center by the origin of the palette? – Divine Propotion May 28 '14 at 07:36
  • Still no luck. I tried writing the program from scratch. Distance is much larger than the radius of circleView. http://paste.ubuntu.com/7535045/ – Divine Propotion May 28 '14 at 08:22
  • You need to show me how you are adding the the 2 views, is the small circle a subview of the palette, or are they both added to a superview – Flexicoder May 29 '14 at 11:01
  • and yes. small circle is a subview of big circle (palette/wheel) – Divine Propotion May 29 '14 at 13:01
  • I have uploaded a sample app with my code. Can you please have a look for me. Thank you for all the help you have provided so far. https://www.dropbox.com/sh/tys09q909kwrq7i/AACrZ8Ss3OJJpXWutI5Gtgjoa/ios-app – Divine Propotion May 29 '14 at 14:02