With a little help from the last question regarding drawings in Cocoa i’ve implemented some basic shapes, as well as dragging / resizing.
So, right now i’m trying to figure out, how to create a effect like in Keynote when a shape is resized and it automatically fits the size of another shape next to it and then “locks” the mouse for a bit of time.
The first attempt is to use a delay function, like
NSDate *future = [NSDate dateWithTimeIntervalSinceNow: 0.5 ];
[NSThread sleepUntilDate:future];
reacting on the desired event (e. g. shape width == height). But this results not in the desired effect, since the whole App freezes for the specified amount of time. In addition to that i think, that the user won’t recognize it as something saying “you’ve reached a special size”. Showing guidelines only at the event is not a solution, since the guidelines are shown as soon as the shape is selected.
For snap to guides, I don’t think you actually want the cursor to stop. Just that the resizing should stop reacting to the cursor movements, within a small range of your target.
The solution in that other question is more or less what you want, I think. Essentially, when you get close enough to the guide, you just change the point’s coordinates to those of the guide. So, building on the sample code I posted in your earlier question, this becomes your view’s
mouseDragged:, andmouseUp:. You can leave the new checks out ofmouseDragged:if you want the point to snap only on mouse up, a different but just as valid behavior.If you’re matching the edges of rectangles, you’ll probably find the Foundation Rect Functions, like
NSMaxXandNSMaxY, useful.