0

我有一个 UIView,然后我使用 CoreGraphics 绘制了一个几何图形。这个几何图形被添加为 UIView 的子视图。我打算使用 UIPanGestureRecognizer 来移动绘图。是否可以保持 UIView 并仅移动设计?

4

3 回答 3

1

写下这段代码。

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];

    [panRecognizer setDelegate:self];
    [yourSubview addGestureRecognizer:panRecognizer];



- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
    UIView *piece = [gestureRecognizer view];



    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        CGPoint translation = [gestureRecognizer translationInView:[piece superview]];

        [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
        [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];

    }

}

在上述方法中实现您的代码。这将对您有所帮助。

于 2012-05-24T13:16:21.473 回答
0

如果您的绘图视图是 a UIView,您可以随时添加 aUIPanGestureRecognizer来移动它,就像这样:

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(drawingMoved:)];    
[_drawingView addGestureRecognizer:pan];
[pan release];

像这样处理运动:

- (void)drawingMoved:(UIGestureRecognizer *)sender
{
    NSUInteger numOfTouch = [sender numberOfTouches];
    if (numOfTouch==1) {
        CGPoint point = [sender locationInView:_parentView];
        UIView *drawingView = sender.view;
        drawingView.center = point;
    }    
}
于 2012-05-24T13:01:43.640 回答
0

您可以编写 if 条件来过滤移动对象。在此链接上查看我的帖子单击此处:-

谢谢你!

于 2012-05-24T13:33:01.973 回答