【问题标题】:How can I use pinch zoom(UIPinchGestureRecognizer) to change width of a UITextView?如何使用捏缩放(UIPinchGestureRecognizer)来改变 UITextView 的宽度?
【发布时间】:2011-02-11 02:13:10
【问题描述】:

我可以让UIPinchGestureRecognizer 处理程序用于缩放对象,但我不想缩放我想更改大小。例如,我有一个 UITextView 并且我已经附加了一个 UIPinchGestureRecognizer 手势,如果用户捏我想改变 textview 的宽度以匹配捏。我不想缩放它,所以UITextView 更大(缩放)。

【问题讨论】:

  • 你不应该攻击手无寸铁的 UIPinchGestureRecognizers :P
  • @Jongsma 为什么不呢?它可能先捏他!
  • 我认为他的意思是 PunchGestureRecognizer :)

标签: iphone objective-c ipad pinch


【解决方案1】:

你必须在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】:

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

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

    【讨论】:

      【解决方案3】:

      我也在做同样的事情。 如果我找到了怎么做,我会更新这篇文章。

      试试这个,它对我有用(对于 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);想知道为什么我的绑定大小没有改变几个小时......非常感谢!
      【解决方案4】:

      我认为您想要做的只是将 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+
      • 奇怪...我没想到会这样。我从来没有真正使用过手势识别器......
      • 为什么不删除这个答案?
      • 当手势状态改变时,您可以在某些因素上划分比例;这将有助于减缓增长
      猜你喜欢
      • 2012-11-30
      • 2014-04-08
      • 1970-01-01
      • 2015-04-25
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 2014-04-07
      • 1970-01-01
      相关资源
      最近更新 更多