Iphone IOS:在父视图上移动子视图
我是IOS的新手。 在我的应用程序中,每次用户绘制圆时,我都会创建圆的新子视图。 创建圆后,我希望用户能够在父视图上拖动圆。Iphone IOS:在父视图上移动子视图,iphone,ios,view,touch,Iphone,Ios,View,Touch,我是IOS的新手。 在我的应用程序中,每次用户绘制圆时,我都会创建圆的新子视图。 创建圆后,我希望用户能够在父视图上拖动圆。 如果用户创建多个圆,我如何知道触摸了哪个圆,并在屏幕上拖动此特定圆。您可以将平移手势添加到您要移动的对象: // Add UIPanGestureRecognizer to each circle view u add and set a tag for each circle UIPanGestureRecognizer *panRecognizer = [[UIPan
如果用户创建多个圆,我如何知道触摸了哪个圆,并在屏幕上拖动此特定圆。您可以将平移手势添加到您要移动的对象:
// 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];
}
}
您可以将平移手势添加到要移动的对象:
// 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];
}
}
我已经成功地实现了我想要实现的目标。我在我的
圆圈中覆盖touchmoved
方法
子视图
并在那里输入此行:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
self.center = [mytouch locationInView:self.superview];
}
现在,创建的每个
圆圈
,我都可以将其与屏幕一起拖动。我已经成功实现了我想要实现的目标。我在我的圆圈中覆盖touchmoved
方法
子视图
并在那里输入此行:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
self.center = [mytouch locationInView:self.superview];
}
现在,每个创建的
圆圈
,我都可以将其与屏幕一起拖动。你的圆圈应该是一个视图,因此给你的圆圈添加标签,并根据标签区分每个圆圈获取标签。但我还是不能移动它们为什么你能显示一些代码?我想我找到了一个非常简单和优雅的方法。你的圆圈应该是一个视图,所以给你的圆圈贴上标签,并根据标签区分。每个圆圈都要贴上标签。但我还是不能移动它们为什么你能显示一些代码?我想我找到了一个非常简单和优雅的方法。我会贴出来的。