2012-02-23 22 views
15

मैं ऐसे गेम पर काम कर रहा हूं जिसमें ऑब्जेक्ट पर लंबे समय से दबाकर गेम ऑब्जेक्ट की विशेषता सेट की जाती है। विशेषता का मूल्य लंबे प्रेस इशारे की अवधि के आधार पर निर्धारित किया जाता है। मैं इस उद्देश्य के लिए UILongPressGestureRecognizer उपयोग कर रहा हूँ, तो यह कुछ इस तरह है:आईओएस: लंबी प्रेस इशारा की अवधि कैसे प्राप्त करें?

[gameObjectView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] 
             initWithTarget:self action:@selector(handle:)]]; 

फिर हैंडलर समारोह

- (void)handle:(UILongPressGestureRecognizer)gesture { 
    if (gesture.state == UIGestureRecognizerStateEnded) { 
    // Get the duration of the gesture and calculate the value for the attribute 
    } 
} 

मैं कैसे इस मामले में देर तक दबाए रखने इशारा की अवधि मिलता है?

उत्तर

26

मुझे पूरा यकीन है कि इशारा आपके लिए इस जानकारी को स्टोर करने के लिए संग्रहीत नहीं करता है। आप केवल उस पर एक संपत्ति सेट कर सकते हैं जिसे न्यूनतम प्रेस अवधि कहा जाता है जो इशारा पहचाने जाने से पहले समय की मात्रा है। ios 5 (untested) के साथ

वर्कअराउंड: @property (nonatomic, strong) NSTimer *timer;

और एक काउंटर: @property (nonatomic, strong) int counter;

फिर @synthesize

- (void)incrementCounter { 
    self.counter++; 
} 

- (void)handle:(UILongPressGestureRecognizer)gesture { 
    if (gesture.state == UIGestureRecognizerStateBegan) { 
     self.counter = 0; 
     self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(incrementCounter) userInfo:nil repeats:yes]; 
    } 
    if (gesture.state == UIGestureRecognizerStateEnded) { 
     [self.timer invalidate]; 
    } 
} 

तो जब इशारा

एक NSTimer संपत्ति कहा जाता टाइमर बनाएं एक टाइमर शुरू होता है जो इशारा अंत तक हर सेकेंड में वृद्धिशीलता विधि को सक्रिय करता है रों। इस मामले में आप minimumPressDuration को 0 पर सेट करना चाहते हैं अन्यथा इशारा सीधे शुरू नहीं होगा। फिर काउंटर के साथ जो कुछ भी आप चाहते हैं!

+0

तो इशारा अवधि प्राप्त करने के लिए कोई कामकाज है? –

+0

उपरोक्त कोड को चेक करें। उम्मीद है कि यह आपके लिए काम करेगा! यदि आप आईओएस 5 का उपयोग नहीं कर रहे हैं तो आपको कुछ बदलाव करने की आवश्यकता हो सकती है। –

+0

मैं सिर्फ इस उद्देश्य के लिए एक संपत्ति बनाने से परहेज कर रहा था, लेकिन मुझे लगता है कि कोई दूसरा रास्ता नहीं है। मैं एनएसटीमर के बजाय एनएसडीएट का उपयोग करता हूं ताकि कोड क्लीनर हो सके। आपके उत्तर के लिए धन्यवाद! –

2

"न्यूनतम प्रेस अवधि" संपत्ति देखें। प्रलेखन के अनुसार:

न्यूनतम अवधि उंगलियों के इशारे के लिए दृश्य पर प्रेस चाहिए मान्यता प्राप्त हो।

[...]

समय अंतराल सेकंड में है। डिफ़ॉल्ट अवधि 0.5 सेकंड है।

+0

सवाल अवधि के बारे में है, यानि कि कितनी देर तक उपयोगकर्ता अपने उंगली आयोजित नीचे, minimumPressDuration देरी सेट करने से पहले इशारा शुरू हो रहा है के लिए है। –

1

मुझे पता है कि यह एक देर का जवाब है, लेकिन यह टाइमर बनाने के बिना आईओएस 7 & 8 में मेरे लिए पूरी तरह से काम करता है।

UILongPressGestureRecognizer *longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(yourAction)]; // create the recognizer 
longGR.minimumPressDuration = 10.0f; //Ten Seconds 
longGR.allowableMovement = 50.0f; //Allowable Movement while being pressed 
[gameObjectView setUserInteractionEnabled:YES]; //If setting interaction to a non-interactable object such as a UIImageView 
[gameObjectView addGestureRecognizer:longGR]; //Add the gesture recognizer to your object 
5

यह अब तक लगता है वस्तु उन्मुख कोको टच में साफ और सरल समाधान UILongPressGesture उपवर्ग है। स्विफ्ट में लिखा गया एक उदाहरण यहां दिया गया है।

class MyLongPressGesture : UILongPressGestureRecognizer { 
     var startTime : NSDate? 
    } 

    func installGestureHandler() { 
      let longPress = MyLongPressGesture(target: self, action: "longPress:") 
      button.addGestureRecognizer(longPress) 
    } 

    @IBAction func longPress(gesture: MyLongPressGesture) { 
      if gesture.state == .Began { 
        gesture.startTime = NSDate() 
      } 
      else if gesture.state == .Ended { 
        let duration = NSDate().timeIntervalSinceDate(gesture.startTime!) 
        println("duration was \(duration) seconds") 
      } 
    } 

आप पहली बार नल से समय शामिल करना चाहते हैं, तो आप इसे शामिल जब तुम अवधि की गणना, gesture.minimumPressDuration वापस जोड़कर कर सकते हैं। दोष यह है कि यह शायद माइक्रो-सेकेंड सटीक नहीं है, क्योंकि इशारा होने वाले इशारे के बीच एक छोटा (छोटा) समय बीतने की संभावना है और आपके। हैंडलर को बुलाया जा रहा है। लेकिन अधिकांश अनुप्रयोगों के लिए जो कोई फर्क नहीं पड़ता।

6

कोई टाइमर आवश्यक नहीं है।आप इस तरह से इसे प्राप्त कर सकते हैं:

- (void)handleRecognizer:(UILongPressGestureRecognizer *)gesture 
{ 
    static NSTimeInterval pressStartTime = 0.0; //This an be moved out and be kept as a property 

    switch ([gesture state]) 
    { 
     case UIGestureRecognizerStateBegan: 
      //Keeping start time... 
      pressStartTime = [NSDate timeIntervalSinceReferenceDate]; 
      break; /* edit*/ 
     case UIGestureRecognizerStateEnded: 
     { 
      //Calculating duration 
      NSTimeInterval duration = [NSDate timeIntervalSinceReferenceDate] - pressStartTime; 
      //Note that NSTimeInterval is a double value... 
      NSLog(@"Duration : %f",duration); 
      break; 
     } 
     default: 
      break; 
    } 
} 

भी इशारा पहचानकर्ता के minimumPressDuration0 जबकि इसे बनाने सेट करने के लिए मत भूलना, आप देर तक दबाए रखने के वास्तविक अवधि पाने के लिए चाहते हैं:
myLongPressGestureRecognizer.minimumPressDuration = 0

1

आप इसे में स्विफ्ट 3,0

तर्क के बाद से प्राप्त कर सकते हैं: बस प्रेस के समय बताए और समय क का अंतर मिल स्पर्श समाप्त होता है एन

कोड:

//variable to calculate the press time 
static var pressStartTime: TimeInterval = 0.0 

func handleRecognizer(gesture: UILongPressGestureRecognizer) -> Double { 
    var duration: TimeInterval = 0 

    switch (gesture.state) { 
    case .began: 
     //Keeping start time... 
     Browser.pressStartTime = NSDate.timeIntervalSinceReferenceDate 

    case .ended: 
     //Calculating duration 
     duration = NSDate.timeIntervalSinceReferenceDate - Browser.pressStartTime 
     //Note that NSTimeInterval is a double value... 
     print("Duration : \(duration)") 

    default: 
     break; 
    } 

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