简体   繁体   English

iOS:在父视图上移动子视图

[英]IOS: moving subviews on parent view

Im new at IOS. 我是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: 我在circle subview覆盖了touchmoved方法,并在其中输入以下行:

-(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. 现在,每个创建的circle都可以与屏幕一起拖动。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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