我是IOS的新人。 在我的应用程序中,每次用户绘制圆圈时,我都会创建一个新的圆形子视图。 在创建圆之后,我希望用户能够在父视图上拖动圆。 如果用户创建了多个圆圈,我怎么知道触摸了哪个圆圈并在屏幕上拖动这个特定的圆圈。
答案 0 :(得分:1)
我成功实现了我想要实现的目标。我在touchmoved
circle
中覆盖了subview
方法,并在那里输入以下内容:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
self.center = [mytouch locationInView:self.superview];
}
现在创建的每个circle
,我都可以将其与屏幕一起拖动。
答案 1 :(得分:0)
您可以将Pan手势添加到您想要移动的内容中:
// 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];
}
}