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

发布于 2024-08-30 01:31:11 字数 193 浏览 7 评论 0原文

我可以让 UIPinchGestureRecognizer 处理程序来缩放对象,但我不想缩放,我想更改大小。例如,我有一个 UITextView,并附加了一个 UIPinchGestureRecognizer 手势,如果用户捏合,我想更改文本视图的宽度以匹配捏合。我不想缩放它,因此 UITextView 更大(缩放)。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情丝乱 2024-09-06 01:31:11

我也在做同样的事情。
如果我发现如何做到这一点,我会更新这篇文章。

试试这个,它对我有用(对于 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 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;
}
愁杀 2024-09-06 01:31:11

你必须快速使用:

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

     }
  }

You have to use in swift:

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

     }
  }
野稚 2024-09-06 01:31:11

我认为你想要做的只是将 textView 框架的宽度乘以手势识别器的比例:

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

或者这不是你的意思?

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?

别想她 2024-09-06 01:31:11

我有四个处理文本字段收缩的例程。手势识别器是核心例程。它会查看选定的文本字段是否会被捏出屏幕,我不希望这样。如果不是,那么我会告诉它用手势中的刻度来捏自己。如果选择了多个,我会向那些不会在屏幕上捏自己的人发送通知。

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文