Reputation: 79
Im new at IOS. in my app, every time the user is drawing a circle i create a new subview of the circle. after the circle is created i would like the user to be able to drag the circle on the parent view. if the user create more then one circle, how can i know which circle was touched and to drag this specific circle on the screen.
Upvotes: 1
Views: 1087
Reputation: 79
i have succeeded in what i wanted to achieve. i override touchmoved
method in my circle
subview
and enter this line there:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
self.center = [mytouch locationInView:self.superview];
}
Now every circle
that is created, i can drag that along with the screen.
Upvotes: 1
Reputation: 6954
You can add Pan gesture to what u want to move:
// Add UIPanGestureRecognizer to each circle view u add and set a tag for each circle
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @selector(handlePan:)];
[view addGestureRecognizer: panRecognizer];
-(void) handlePan: (UIGestureRecognizer *)sender
{
UIPanGestureRecognizer *panRecognizer = (UIPanGestureRecognizer *)sender;
UIView *view = sender.view;
// You can get to know which circle was moved by getting tag of this view
if (panRecognizer.state == UIGestureRecognizerStateBegan ||
panRecognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint currentPoint = self.center;
CGPoint translation = [panRecognizer translationInView: self.superView];
view.center = CGPointMake(currentPoint.x + translation.x, currentPoint.y + translation.y);
[panRecognizer setTranslation: CGPointZero inView: self.view];
}
}
Upvotes: 0