Sandy_ios
Sandy_ios

Reputation: 667

Implementing pinch effect on a image view in iphone

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.

Upvotes: 1

Views: 2196

Answers (2)

Alexander Farber
Alexander Farber

Reputation: 23058

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

Upvotes: 0

Greg
Greg

Reputation: 9178

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

Upvotes: 2

Related Questions