2013-02-06 6 views
7

से टच जेस्चर के प्रकार की पहचान करना क्या UIEvent को ट्रिगर करने वाले स्पर्श संकेतों के प्रकार को प्राप्त करने का कोई तरीका है? तो, मान लें कि मैं हिटटेस्ट के साथ एक इशारा रोकता हूं: साथ: इवेंट:। मैं कैसे पहचान सकता हूं कि किस प्रकार का स्पर्श (चुटकी, टैप, स्वाइप, इत्यादि) इस विधि को ट्रिगर किया?UIEvent ऑब्जेक्ट

मैं UIEvent उप प्रकार से दूरस्थ घटनाओं के प्रकार प्राप्त कर सकते हैं, और यहां तक ​​कि अगर डिवाइस हिल गया था, लेकिन वहाँ स्पर्श घटनाओं के लिए कोई पैरामीटर है ...

आप उन पहचान कैसे करूं?

+0

@blub: वास्तव में, यह कोई डुप्लिकेट है - प्रश्न यहाँ hitTest में के बारे में निम्न स्तर को छूने का पता लगाने है: withEvent: – sergio

+0

संभव "मेरी गलती, खेद" – blub

उत्तर

5

आपको अपने आप पर स्पर्श विश्लेषण करना है।

यह मुश्किल नहीं है, लेकिन यह आपके लिए नहीं किया जाता है। यहां कुछ कोड से नमूना है जिसका मैं वास्तव में उपयोग कर रहा हूं। यह पूर्ण पैमाने पर समाधान के रूप में नहीं है। यह केवल एक बहुत ही ठोस संदर्भ (मेरे ऐप की) में मूल टैप/स्वाइप/चुटकी संकेतों का पता लगाता है, और इशारा (अधिसूचनाएं) देने के लिए एक अनकोल तंत्र का उपयोग करता है। इसलिए, यह आपके मामले पर लागू हो सकता है या अधिक संभावना नहीं है, लेकिन मुझे उम्मीद है कि यह आपको आवश्यक जानकारी का एक विचार देगा।

NSSet* allTouches = [event allTouches]; 
UITouch* touch = [allTouches anyObject]; 
UIView* touchView = [touch view]; 

     if (touch.phase == UITouchPhaseBegan) { 

      _initialView = touchView; 
      startTouchPosition1 = [touch locationInView:self]; 
      startTouchTime = touch.timestamp; 

      if ([allTouches count] > 1) { 
       startTouchPosition2 = [[[allTouches allObjects] objectAtIndex:1] locationInView:self]; 
       previousTouchPosition1 = startTouchPosition1; 
       previousTouchPosition2 = startTouchPosition2; 
      } 
     } 

     if (touch.phase == UITouchPhaseMoved) { 

      if ([allTouches count] > 1) { 
       CGPoint currentTouchPosition1 = [[[allTouches allObjects] objectAtIndex:0] locationInView:self]; 
       CGPoint currentTouchPosition2 = [[[allTouches allObjects] objectAtIndex:1] locationInView:self]; 

       CGFloat currentFingerDistance = CGPointDist(currentTouchPosition1, currentTouchPosition2); 
       CGFloat previousFingerDistance = CGPointDist(previousTouchPosition1, previousTouchPosition2); 
       if (fabs(currentFingerDistance - previousFingerDistance) > ZOOM_DRAG_MIN) { 
        NSNumber* movedDistance = [NSNumber numberWithFloat:currentFingerDistance - previousFingerDistance]; 
        if (currentFingerDistance > previousFingerDistance) { 
//       NSLog(@"zoom in"); 
         [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ZOOM_IN object:movedDistance]; 
        } else { 
//       NSLog(@"zoom out"); 
         [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ZOOM_OUT object:movedDistance]; 
        } 
       } 
      } 
     } 

     if (touch.phase == UITouchPhaseEnded) { 
      CGPoint currentTouchPosition = [touch locationInView:self]; 

      // Check if it's a swipe 
      if (fabsf(startTouchPosition1.x - currentTouchPosition.x) >= SWIPE_DRAG_HORIZ_MIN && 
       fabsf(startTouchPosition1.x - currentTouchPosition.x) > fabsf(startTouchPosition1.y - currentTouchPosition.y) && 
       touch.timestamp - startTouchTime < 0.7) 
      { 
       // It appears to be a swipe. 
       if (startTouchPosition1.x < currentTouchPosition.x) { 
         NSLog(@"swipe right"); 
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SWIPE_RIGHT object:self]; 
       } else { 
         NSLog(@"swipe left"); 
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SWIPE_LEFT object:self]; 
       } 
      } else { 
       //-- else, check if it's a single touch 
       if (touch.tapCount == 1) { 
        NSDictionary* uInfo = [NSDictionary dictionaryWithObject:touch forKey:@"touch"]; 
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_TAP object:self userInfo:uInfo];       
       }/* else if (touch.tapCount > 1) { 
        handle multi-touch 
       } 
       */ 
      } 

      startTouchPosition1 = CGPointMake(-1, -1); 
      _initialView = nil; 
     } 

     if (touch.phase == UITouchPhaseCancelled) { 
      _initialView = nil; 
//   NSLog(@"TOUCH CANCEL"); 
     } 
+0

मैं इसे एक परीक्षण चलाने के लिए और रिपोर्ट दे देंगे थोड़ी देर में वापस। धन्यवाद! – user1799795

+0

कृपया, मान लें कि मेरा कोड सिर्फ एक स्निपेट है: यह पूर्ण पैमाने पर समाधान के रूप में नहीं है। यह केवल एक बहुत ही ठोस संदर्भ (मेरे ऐप की) में मूल टैप/स्वाइप/चुटकी संकेतों का पता लगाता है, और इशारा (अधिसूचनाएं) देने के लिए एक अनकोल तंत्र का उपयोग करता है। मुझे उम्मीद है कि यह आपको एक विचार देता है कि क्या आवश्यक है। – sergio

+0

मुझे इसके बारे में पता है। :) – user1799795

0

आपको स्पर्शों (स्पर्शों और वहां की स्थिति की गिनती) का विश्लेषण करने की आवश्यकता है। एक और आसान तरीका संबंधित प्रकार प्राप्त करने के लिए संबंधित इशारा पहचानकर्ता को जोड़ना है। सभी टच इवेंट का इवेंट प्रकार UIEventSubtypeNone है।

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