Gypsa
Gypsa

Reputation: 11314

Draw a rectangle with center as touch point

I am working on a project in which I have to create a tagging screen.What exactly I have to do is when the user touch on the image a rectangle should be drawn assuming the center points be the touch points.Please suggest me the approach or some sample code which I should follow it. Any suggestions will be highly appreciated. Thanks in advance.

Upvotes: 1

Views: 1083

Answers (2)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

There are few ways to do this but I prefer the idea of using layers for this task. For this to work, you will have to attach a tap recognizer to the image view and add the tag layers as sublayers to the image view's layer in the gesture handler.

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[imageView addGesture:tap];
[tap release];

...

- (void)handleGesture:(UITapGestureRecognizer*)gesture {
    CALayer *newLayer = [CALayer layer];
    layer.bounds = layerBounds;
    layer.position = [gesture locationInView:gesture.view];
    layer.backgroundColor = layerBackgroundColor;

    [imageView.layer addSublayer:layer];
}

You will have to add the QuartzCore framework for this to work and #import <QuartzCore/QuartzCore.h> for this to work.

Upvotes: 1

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31720

you could change the center point of an UIView using center point property ,

Change center property of your view and redraw it.

Upvotes: 1

Related Questions