Reputation: 1938
the rectangle will be considered two points, first point will be the touchBegan point, and on touchMove will be the second point, the rectangle will be dynamically drawn according to the user finger move, (like when you click one click on desktop and move the mouse you will get dynamically rectangle).
Thanks
Upvotes: 3
Views: 4606
Reputation: 1867
Okay here is how you can draw your rect in touchesMoved
(use @Nekto touchesBegan
to store the starting point of your rect).
Let's assume you keep a reference on the UIView we are drawing and call it drawnView
.
In touchesBegan
, we set the origin of the frame for drawnView
Here is touchesMoved
:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent*)event
{
// We only take care of a single touch
if ([touches count] == 1)
{
UITouch *touch = [touches anyObject];
// Find the width and height of the rect
CGRect drawnFrame = drawnView.frame
drawnFrame.size.width = [touch locationInView:parentView].x - drawnFrame.origin.x
drawnFrame.size.height = [touch locationInView:parentView].y - drawnFrame.origin.y
[drawnView setFrame:drawnFrame];
}
}
Don't copy paste that code, I didn't write it in Xcode or anything. It could have mistakes (for sure). But I hope it can help to find your solution. And be careful, I can't tell how it will behave if you drag your finger to the top, or to the left (negative values for height and width).
Upvotes: 4
Reputation: 17877
UIView
, for example, DrawingView
In DrawingView
you should implement following methods:
Save start point in method: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
Redraw rect here - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
Hide rect (or save it) here - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
and here - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
For example, in first method you could detect start point in following way:
CGPoint startCornerOfYourRect;
for (UITouch *touch in touches)
{
startCornerOfYourRect = [touch locationInView:self];
break;
}
Upvotes: 4