我是 IOS 的新手。在我的应用程序中,每次用户画一个圆圈时,我都会创建一个新的圆圈子视图。创建圆圈后,我希望用户能够在父视图上拖动圆圈。如果用户创建了一个以上的圆圈,我怎么知道哪个圆圈被触摸并在屏幕上拖动这个特定的圆圈。
问问题
1058 次
2 回答
1
我已经成功实现了我想要实现的目标。我覆盖touchmoved
我的方法circle
subview
并在那里输入这一行:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
self.center = [mytouch locationInView:self.superview];
}
现在circle
创建的每一个,我都可以将它与屏幕一起拖动。
于 2013-07-03T06:50:52.543 回答
0
您可以将平移手势添加到您想要移动的位置:
// 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];
}
}
于 2013-07-02T12:38:29.183 回答