2010-04-22 9 views
7

मैं UIPinchGestureRecognizer हैंडलर को ऑब्जेक्ट स्केल करने के साथ काम करने के लिए प्राप्त कर सकता हूं लेकिन मैं स्केल नहीं करना चाहता हूं कि मैं आकार बदलना चाहता हूं। उदाहरण के लिए मेरे पास UITextView है और मैंने UIPinchGestureRecognizer इशारा किया है और यदि उपयोगकर्ता पिन करता है तो मैं चुटकी से मेल खाने के लिए टेक्स्टव्यू की चौड़ाई बदलना चाहता हूं। मैं इसे स्केल नहीं करना चाहता हूं इसलिए UITextView बड़ा है (ज़ूमिंग)।UITextView की चौड़ाई बदलने के लिए मैं चुटकी ज़ूम (UIPinchGestureRecognizer) का उपयोग कैसे कर सकता हूं?

+31

आप निराश्रित UIPinchGestureRecognizers पर हमला नहीं करना चाहिए: पी –

+0

@KennyTM Haha! अच्छा एक ... –

+2

@ जोंगस्मा क्यों नहीं? यह उसे पहले चुरा लिया होगा! –

उत्तर

0

मुझे लगता है कि आप क्या करना सिर्फ इशारा पहचानकर्ता के पैमाने के साथ अपने TextView के फ्रेम की चौड़ाई गुणा किया जाता है चाहता हूँ:

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

या यह तुम क्या मतलब नहीं है?

+0

ऐसा लगता है कि यह काम नहीं कर रहा है। Uitextview बहुत तेजी से बढ़ता है और देरी हो रही है। स्केल 0-20 + –

+0

से हो सकता है अजीब ... मुझे इसकी उम्मीद नहीं होती। मैंने वास्तव में कभी भी इशारा पहचानकर्ताओं का उपयोग नहीं किया है ... –

+7

इस उत्तर को क्यों न हटाएं? –

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; 
} 
+0

क्या वास्तव में मेरी मदद की गई _view.bounds = CGRectApplyAffineTransform (प्रारंभिक बाउंड्स, zt); घंटों तक आश्चर्य हुआ कि मेरा बाध्य आकार क्यों नहीं बदला .. बहुत बहुत धन्यवाद! – Oritm

+0

इस कोड में बहुत सी त्रुटियां हैं। इस कोड को चलाने पर – user2083364

+0

हमेशा क्रैश हो गया ... – HoangNA

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

} 
2

आप तेजी में उपयोग करने के लिए:

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

    } 
    } 
संबंधित मुद्दे