保持UIView并仅移动子视图

时间:2012-05-24 12:56:38

标签: iphone ios5 uiview subview uipangesturerecognizer

我有一个UIView,我使用CoreGraphics做了一个几何图形的绘图。这个几何图形被添加为UIView的子视图。 我打算使用UIPanGestureRecognizer来移动绘图。可以抓住UIView并移动设计吗?

3 个答案:

答案 0 :(得分: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]];

    }

}

使用上述方法实现您的代码。这会对你有所帮助。

答案 1 :(得分:0)

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

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;
    }    
}

答案 2 :(得分:0)

你可以编写if过滤移动对象的条件。 请参阅此链接click here上的帖子: -

谢谢你!