简体   繁体   中英

Hold the UIView and move only the subview

I have a UIView and over I did a drawing of a geometric figure using CoreGraphics. This geometric figure is added as subview of UIView. I intend to use a UIPanGestureRecognizer to move the drawing. Is possible hold the UIView and move just the design?

Write this code.

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

    }

}

Implement your code in above method. It will be helpful to you.

If your drawing view is a UIView , you can always add a UIPanGestureRecognizer to move it around, like this way:

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

Handle the movement like this:

- (void)drawingMoved:(UIGestureRecognizer *)sender
{
    NSUInteger numOfTouch = [sender numberOfTouches];
    if (numOfTouch==1) {
        CGPoint point = [sender locationInView:_parentView];
        UIView *drawingView = sender.view;
        drawingView.center = point;
    }    
}

you can write if condition for filtering the moving objects. see my post at this link click here :-

thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM