开发者

Draw a rectangle with center as touch point

I am work开发者_JAVA技巧ing 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.


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.


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

Change center property of your view and redraw it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜