[英]IOS: moving subviews on parent view
我是IOS的新手。 在我的应用中,每次用户绘制一个圆时,我都会创建一个新的圆子视图。 创建圆后,我希望用户能够在父视图上拖动圆。 如果用户创建了一个以上的圆圈,我如何知道触摸了哪个圆圈并将其拖动到屏幕上。
我已经成功实现了自己想要的目标。 我在circle
subview
覆盖了touchmoved
方法,并在其中输入以下行:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
self.center = [mytouch locationInView:self.superview];
}
现在,每个创建的circle
都可以与屏幕一起拖动。
您可以将平移手势添加到要移动的位置:
// 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.