views:

1517

answers:

3

I can get the UIPinchGestureRecognizer handler to work with scaling an object but I don't want to scale I want to change the size. For example I have a UITextView and I've attacked a UIPinchGestureRecognizer gesture to it and if the user pinches I want to change the width of the textview to match the pinch. I don't want to scale it so the uitextview is larger(zooming).

A: 

I think what you want to do is just multiplying the width of your textView's frame with the gesture recognizer's scale:

CGFloat scale = gestureRecognizer.scale;
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(scale*newFrame.size.width, newFrame.size.height);
textView.frame = newFrame;

Or isn't this what you mean?

Jongsma
That doesn't seem to work. The uitextview grows very quickly and is delayed.The scale can be from 0-20+
Ryan Detzel
Odd... I wouldn't have expected that. I have never really used gesture recognizers...
Jongsma
A: 

The default behaviour for a 'Pinch' Gesture is to Zoom in, according to the Documentation. So I think you might have to manually catch the touch events & calculate your own Pinch in the touchesMoved event.

i.e.

#pragma mark -
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([touches count] == 2) {
        NSArray *twoTouches = [touches allObjects];
        UITouch *first = [twoTouches objectAtIndex:0];
        UITouch *second = [twoTouches objectAtIndex:1];
        initialDistance = distanceBetweenPoints(
                                                [first locationInView:self.view], 
                                                [second locationInView:self.view]);
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

  if ([touches count] == 2) {
    NSArray *twoTouches = [touches allObjects];
    UITouch *first = [twoTouches objectAtIndex:0];
    UITouch *second = [twoTouches objectAtIndex:1];
    CGFloat currentDistance = distanceBetweenPoints(
                                                    [first locationInView:self.view],
                                                    [second locationInView:self.view]);

    if (initialDistance == 0)
        initialDistance = currentDistance; 
    else if (currentDistance - initialDistance > kMinimumPinchDelta) {
        label.text = @"Outward Pinch";
        [self performSelector:@selector(eraseLabel) 
                   withObject:nil 
                   afterDelay:1.6f];
    }
    else if (initialDistance - currentDistance > kMinimumPinchDelta) {
        label.text = @"Inward Pinch";
        [self performSelector:@selector(eraseLabel) 
                   withObject:nil 
                   afterDelay:1.6f];
    }
  }
}

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    initialDistance = 0;
 }

- (void)eraseLabel {
    label.text = @"";
}
ThePaddedCell
NOTE: This code is taken from the http://iphonedevbook.com sample code.
ThePaddedCell
Kind of works but since it's a uitextview the text starts to get selected and it stops the interaction. It's not editable but selection still works.
Ryan Detzel
A: 

I am doing the very same thing. I will update this post if I found how to do it.

Try this, it work for me (for UIView):

- (IBAction)handlePinchGesture:(UIGestureRecognizer *)sender {
    static CGRect initialBounds;

    UIView *_view = sender.view;

    if (sender.state == UIGestureRecognizerStateBegan)
    {
        initialBounds = _view.bounds;
    }
    CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];

    CGAffineTransform zt = CGAffineTransformScale(CGAffineTransformIdentity, factor, factor);
    _view.bounds = CGRectApplyAffineTransform(initialBounds, zt);
    return;
}
olipion