1

Thanks in advance. I want to apply pinch, rotation and pan gesture effect on an image view.like this

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scalePiece:)];
    [imgView addGestureRecognizer:pinchGesture];
    [pinchGesture release];

    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotatePiece:)];
    [imgView addGestureRecognizer:rotationGesture];
    [rotationGesture release];

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panPiece:)];
    [imgView addGestureRecognizer:panGesture];
    [panGesture release];

// Method implementation

- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        UIView *piece = gestureRecognizer.view;
        CGPoint locationInView = [gestureRecognizer locationInView:piece];
        CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];

        piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
        piece.center = locationInSuperview;
    }
}
- (void)rotatePiece:(UIRotationGestureRecognizer *)gestureRecognizer
{
    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        [gestureRecognizer view].transform = CGAffineTransformRotate([[gestureRecognizer view] transform], [gestureRecognizer rotation]);
         NSLog(@"Rotate : %f",[gestureRecognizer rotation]);
        [gestureRecognizer setRotation:0];
    }
}
- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
    UIView *piece = [gestureRecognizer view];

    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

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

- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer
{
    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer velocity], [gestureRecognizer velocity]);
        [gestureRecognizer setScale:1];
    }
}

In this rotation and swipe(pan) working fine but pinch is not good. I have seen one app i.e.,http://itunes.apple.com/us/app/image-mask-costume-hd/id443821357?mt=8 in this the touch events are very nice . I want to implement like this is it possible by using gesture or we need to follow touch events. Do any one have idea please help me to do it.

2 Answers 2

2

Try this code: http://dl.dropbox.com/u/9397784/Image%20multitouch.txt

Replace currentlyEditing with your view. Just copy the code into the implementation of the view or view controller you want to use it in. The code handles zoom, rotate and pan. To reset the view back to the default position, call yourView.transform = CGAffineTransformIdentity;.

5
  • This method does not use a gesture recognizer, but handles the touch event directly. I find it easier to do it that way. All other objects (buttons, etc.) should still respond to touch after you put this in.
    – Greg
    Jul 14, 2011 at 13:45
  • Thanks for ur response.Yes this is what i exactly want.In this every thing works fine ,But for pinch effect if i am pinching either in x or y direction i want to increase or decrease the view size in that direction only means width or height, not to scale in both directions. I think u got my point.If not clear once download img mask example from app store and check it once please.
    – Sandy_ios
    Jul 15, 2011 at 5:50
  • I see what you mean. Let me check the code and see where that can be changed.
    – Greg
    Jul 15, 2011 at 5:52
  • Use this line: CGAffineTransform newTransform2 = CGAffineTransformScale(currentlyEditing.transform, (curPoint1.x - curPoint2.x) / (prevPoint1.x - prevPoint2.x), (curPoint1.y - curPoint2.y) / (prevPoint1.y - prevPoint2.y)); instead of the current one where newTransform2 is set. This is untested but should work.
    – Greg
    Jul 15, 2011 at 6:01
  • Try this: CGAffineTransformScale(currentlyEditing.transform, abs(curPoint1.x - curPoint2.x) / abs(prevPoint1.x - prevPoint2.x), abs(curPoint1.y - curPoint2.y) / abs(prevPoint1.y - prevPoint2.y));
    – Greg
    Jul 15, 2011 at 12:53
0

The Apple Touches example suggests to use [gestureRecognizer scale] and not [gestureRecognizer velocity]:

- (IBAction)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer
{
    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
        [gestureRecognizer setScale:1];
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.