Comment puis-je utiliser pinch zoom(UIPinchGestureRecognizer) pour changer la largeur d'un UITextView?

je peux obtenir le handler UIPinchGestureRecognizer pour travailler avec la mise à l'échelle d'un objet, mais je ne veux pas mettre à l'échelle, je veux changer la taille. Par exemple, j'ai un UITextView et j'ai attaché un UIPinchGestureRecognizer geste et si l'utilisateur pince je veux changer la largeur de la textview pour correspondre à la pincer. Je ne veux pas le dimensionner pour que le UITextView soit plus grand(zoom).

7
demandé sur Bhanu 2010-04-22 17:50:18

4 réponses

je fais la même chose. Je mettrai à jour ce post si j'ai trouvé comment le faire.

Essayer, cela fonctionne pour moi (pour 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;
}
23
répondu olipion 2010-05-22 13:54:37

vous devez utiliser dans swift:

   func pinchgsterAction(gesture:UIPinchGestureRecognizer){
     if (gesture.state == UIGestureRecognizerState.Changed) {
         let scale:CGFloat = gesture.scale
         gesture.view.transform = CGAffineTransformScale(gesture.view.transform, scale, scale)

     }
  }
2
répondu Leena 2015-09-17 11:10:01

je pense que ce que vous voulez faire est simplement de multiplier la largeur du cadre de votre textView avec l'échelle de reconnaissance du geste:

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

ou n'est-ce pas ce que vous voulez dire?

0
répondu Pieter Jongsma 2010-04-22 14:02:21

j'ai quatre routines qui traitent de pincement d'un champ de texte. La reconnaissance gestuelle est la routine principale. Il voit si le champ de texte sélectionné(s) vont être coincé hors de l'Écran, je n'en veux pas. Si ce n'est pas le cas, je lui dis de se pincer avec la balance du geste. S'il y a plusieurs selected j'envoie une Notification pour ceux qui ne vont pas pincer l'écran pour se pincer eux-mêmes.

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

}
0
répondu scooter133 2011-10-31 16:50:57