如何使用捏合缩放(UIPinchGestureRecognizer)来改变UITextView的宽度?

7
我可以使用UIPinchGestureRecognizer处理程序来缩放对象,但我不想缩放,我想改变大小。例如,我有一个UITextView,并且我已经将UIPinchGestureRecognizer手势附加到它上面,如果用户捏合,我想将文本视图的宽度更改为与捏合匹配的宽度。我不想缩放它,因此UITextView不会变大(缩放)。

31
不该攻击无防御的UIPinchGestureRecognizers :P - Pieter Jongsma
2
@Jongsma 为什么不呢?它可能先夹了他! - James Webster
1
我认为他的意思是PunchGestureRecognizer :) - Amarsh
4个回答

23

我正在做同样的事情。如果我找到如何做到这一点,我将更新此帖子。

尝试这个,对我有用(针对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;
}

真正帮助我的是 _view.bounds = CGRectApplyAffineTransform(initialBounds, zt); 我想了几个小时为什么我的边界大小没有改变.. 非常感谢! - Oritm

2

您必须在Swift中使用:

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

     }
  }

0
我认为你想要做的就是将textView框架的宽度乘以手势识别器的比例:
CGFloat scale = gestureRecognizer.scale;
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(scale*newFrame.size.width, newFrame.size.height);
textView.frame = newFrame;

或者这不是你的意思吗?


似乎不起作用。UITextView增长非常快,而且有延迟。比例可以从0-20+。 - Ryan Detzel
奇怪... 我没想到会这样。我从来没真正使用过手势识别器... - Pieter Jongsma
7
为什么不删除这个回答? - Dan Rosenstark
当手势状态改变时,您可以将比例因子分成几部分;这将有助于减缓增长速度。 - Dannie P

0
我有四个处理文本字段捏合的例程。手势识别器是核心例程。它检查所选的文本字段是否将被捏掉屏幕,如果是,我不希望这样。如果它们没有被捏掉,那么我告诉它使用手势的比例来捏合自己。如果有多个被选中,我会为那些不会被捏掉屏幕的发送通知,让它们自己捏合。
    //--------------------------------------------------------------------------------------------------------
    //    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];
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接