2012-02-23 299 views
15

我正在通過長時間按住對象本身來設置遊戲對象的屬性。屬性的值由長按手勢的持續時間決定。我使用UILongPressGestureRecognizer爲了這個目的,所以它是這樣的:iOS:如何獲得長按手勢的持續時間?

[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

我很確定手勢不存儲這些信息供您訪問。您只能在其上設置一個名爲minimumPressDuration的屬性,該屬性是識別手勢之前的時間量。

解決方法與iOS 5(未經測試):

創建一個NSTimer屬性調用定時器:@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]; 
    } 
} 

所以當手勢開始啓動一個計時器,該計時器每秒觸發增量方法,直到手勢結束秒。在這種情況下,您需要將minimumPressDuration設置爲0,否則手勢不會馬上開始。然後用櫃檯做任何你想做的事!

+0

那麼是否有任何解決方法來獲得手勢持續時間? – 2012-02-23 19:28:08

+0

檢查上面的代碼。希望它能爲你工作!如果您不使用ios 5,則可能需要進行一些更改。 – 2012-02-23 21:33:58

+0

我避免爲此目的創建一個屬性,但我想沒有其他辦法。我使用NSDate而不是NSTimer,因此代碼可以更清晰。感謝您的回答! – 2012-02-24 05:32:44

2

請參閱「minimumPressDuration」屬性。根據文檔:

手指必須按下手勢視圖上的最小時間段才能識別 。

[...]

時間間隔以秒爲單位。默認的持續時間是0.5秒 秒。

+0

的問題是關於時間,即用戶如何長期持有自己的手指下降,minimumPressDuration是被觸發了該姿勢之前設置的延遲。 – 2015-02-17 21:31:19

1

我知道這是一個遲到的答案,但這完全適合我在IOS 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

它似乎到目前爲止,面向對象的Cocoa Touch中最乾淨和最簡單的解決方案是子類UILongPressGesture。這是一個用Swift編寫的例子。

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。缺點是它可能不是精確的微秒,因爲手勢被觸發和你的.Start處理程序被調用之間可能會有一小段(微小的)時間流逝。但對絕大多數不重要的應用程序而言。

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

邏輯繼獲得它:只需按指定的時間和發現時間WH的差異帶觸摸結束

代碼:

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