How can I use pinch zoom(UIPinchGestureRecognizer) to change width of a UITextView?

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

Up vote 2 down vote favorite 1 share g+ share fb share tw.

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

Iphone objective-c ipad pinch link|improve this question edited Apr 22 '10 at 14:05SilentGhost46.1k668118 asked Apr 22 '10 at 13:50Ryan Detzel8302816 60% accept rate.

12 You shouldn't attack defenseless UIPinchGestureRecognizers :P – Jongsma Apr 22 '10 at 13:58 10 @Jongsma: That needs UIPunchGestureRecognizer. – KennyTM Apr 22 '10 at 20:33 @KennyTM Haha! Good one... – Jongsma Apr 22 '10 at 21:18.

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

I have four routines that deal with pinching of a text field. The gesture recognizer is the core routine. It sees if the selected text field(s) are going to be Pinched off the Screen, I don't want that.

If they are not, then I tell it to pinch itself with the scale from the gesture. If there are multiple selected I send a Notification for those that won't pinch off screen to Pinch themselves. //-------------------------------------------------------------------------------------------------------- // pinchElement // Description: Called to di the element scale, in our case, we are adjusting the length.

// //-------------------------------------------------------------------------------------------------------- - (void)pinchElement:(CGFloat)scale { //Grab how big we are now CGRect textFieldBounds = textField. Bounds; //Multiple the Scale of the Pinch by the Width to get our new width. CGFloat newWidth = textFieldBounds.size.

Width * scale; CGFloat widthChange = newWidth - textFieldBounds.size. Width; CGRect newBounds = CGRectMake(0, 0, newWidth, textFieldBounds.size. Height ); textField setBounds: newBounds; textField setCenter: CGPointMake(textField.center.

X + widthChange/2, textField.center. Y) ; self contentSizeChanged; } //-------------------------------------------------------------------------------------------------------- // pinchOffScreen // Description: Called to see if the Pinch Gesture will cause element to go off screen Gesture // //-------------------------------------------------------------------------------------------------------- - (BOOL)pinchOffScreen:(CGFloat)scale { //Grab how big we are now CGRect textFieldBounds = textField. Bounds; //Multiple the Scale of the Pinch by the Width to get our new width.

CGFloat newWidth = textFieldBounds.size. Width * scale; //Figure out our Change in Width so we can calculate our new Zen Center CGRect newElementBounds = CGRectMake(0, 0, newWidth+ kElementFrameOffset*2 + kElementContentFrameOffset*2, textFieldBounds.size. Height + kElementFrameOffset*2 + kElementContentFrameOffset*2); //We want to be sure that we don't size beyond our bounds, find our Parent Origin.

CGRect elementBoundsInSuperView = self convertRect:newElementBounds toView:self superview; CGFloat xPosition = CGRectGetMidX(elementBoundsInSuperView); CGFloat yPosition = CGRectGetMidY(elementBoundsInSuperView); BOOL offScreen = self calcOffEditorFromXposition:xPosition yPosition:yPosition fromBoundsInSuperView:elementBoundsInSuperView; return offScreen; } //-------------------------------------------------------------------------------------------------------- // handlePinchGesture // Description: Called when we get a Pinch Gesture // We want to override the default scaling and set the width. // //-------------------------------------------------------------------------------------------------------- - (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer { if (IoUIDebug & IoUIDebugSelectorNames) { NSLog(@"%@ - %@", INTERFACENAME, NSStringFromSelector(_cmd) ); } // UIView *element = gestureRecognizer view; if (gestureRecognizer state == UIGestureRecognizerStateBegan ) { //We are resizing, Select ourself self selectSelf; } if (gestureRecognizer state == UIGestureRecognizerStateBegan || gestureRecognizer state == UIGestureRecognizerStateChanged) { NSSet *selectedElements = (IoScreenEditorViewController *)UIAppDelegate. IoMainViewController.

CurrentViewController editorContentViewController selectedElements; BOOL aSelectedElementOffscreen = FALSE; for (IoUIScreenElement* element in selectedElements) { if (element pinchOffScreen:gestureRecognizer scale) { aSelectedElementOffscreen = TRUE; break; } } if (!aSelectedElementOffscreen) { self pinchElement:gestureRecognizer scale; // Let others know they are moving if they are selected // Setup our data for the Notification NSMutableDictionary *theUserInfo = NSMutableDictionary alloc initWithCapacity:1 autorelease; theUserInfo setObject:self forKey:@"ElementWithGesture"; NSNumber * scaleAsNumber = NSNumber alloc initWithFloat:gestureRecognizer scale; theUserInfo setValue:scaleAsNumber forKey:@"GestureScale"; theUserInfo setObject:gestureRecognizer forKey:@"TheGestureRecognizer"; scaleAsNumber release; // Post the Group Rotation Notification. NSNotificationCenter defaultCenter postNotificationName:kNCSEGroupPinchGesture object:nil userInfo:theUserInfo; } gestureRecognizer setScale:1; } if (gestureRecognizer state == UIGestureRecognizerStateEnded ) { } } //-------------------------------------------------------------------------------------------------------- // groupHandlePinchGesture: // Description: For a groupPinch Notification. Move it!

Within bounds of course // //-------------------------------------------------------------------------------------------------------- - (void) groupHandlePinchGesture:(NSNotification*)notification{ if (IoUIDebug & IoUIDebugSelectorNames) { NSLog(@"%@ - %@", INTERFACENAME, NSStringFromSelector(_cmd) ); } IoUIScreenElement *element = (IoUIScreenElement *) notification userInfo objectForKey:@"ElementWithGesture"; //UIRotationGestureRecognizer *gestureRecognizer = (UIRotationGestureRecognizer *) notification userInfo objectForKey:@"TheGestureRecognizer"; NSNumber *scaleAsNumber = notification userInfo valueForKey:@"GestureScale"; CGFloat scale = scaleAsNumber floatValue; if (IOFNOTEQUAL(self, element) & self isSelected){ self pinchElement: scale; } }.

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?

That doesn't seem to work. The uitextview grows very quickly and is delayed. The scale can be from 0-20+ – Ryan Detzel Apr 22 '10 at 14:13 Odd... I wouldn't have expected that.

I have never really used gesture recognizers... – Jongsma Apr 22 '10 at 15:16 2 why not delete this answer? – Yar Dec 17 '10 at 21:25.

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 = @""; }.

NOTE: This code is taken from the iphonedevbook.com sample code. – ThePaddedCell Apr 22 '10 at 14:19 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 Apr 22 '10 at 15:00.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions