简体   繁体   中英

IOS: moving subviews on parent view

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.

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.

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];
  }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM