2012-02-23 7 views
15

私はゲームオブジェクトの属性が長いオブジェクト自体に押すことにより設定されたゲームに取り組んでいます。属性の値は、長押しジェスチャの持続時間によって決定されます。私はこのケースで長押しジェスチャーの時間を取得するにはどうすればよい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 
    } 
} 

:それはこのようなものですので、私は、この目的のためにUILongPressGestureRecognizerを使用していますか?

答えて

26

私はあなたがアクセスするためのジェスチャーは、この情報を保存しないかなり確信しています。ジェスチャが認識されるまでの時間であるminimumPressDurationというプロパティのみを設定できます。 5(未テスト)IOSと

回避策:@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プロパティと呼ばれるタイマーを作成します。ジェスチャーが終了するまで毎秒インクリメントメソッドを起動するタイマーの開始を開始するs。この場合、minimumPressDurationを0に設定すると、ジェスチャがすぐに開始されません。その後、カウンターで何でもしてください!

+0

ジェスチャの継続時間を取得するための回避策はありますか? –

+0

上記のコードを確認してください。うまくいけばそれはあなたのために働くでしょう!あなたがios 5を使用していない場合は、いくつか変更する必要があります。 –

+0

私はこの目的のためだけにプロパティを作成することを避けていましたが、他の方法はないと思います。私はNSTimerの代わりにNSDateを使うので、コードはより洗練されたものになるでしょう。ご回答有難うございます! –

2

は "minimumPressDuration" プロパティを参照してください。ドキュメントによると、

最小周期の指は、 へのジェスチャの表示を押す必要があります。

[...]

時間間隔は秒です。デフォルトの継続時間は0.5 秒です。

+0

質問は、ユーザが自分の指を下に開催されたどのくらいの期間、すなわちについてですが、minimumPressDurationは、ジェスチャがトリガーされる前に遅延を設定するためのものです。 –

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をバック追加することにより、期間を計算するとき、あなたはそれを含めることができます。欠点は、おそらくジェスチャーがトリガーされてからスタートハンドラが呼び出されるまでに少しの時間が経過すると仮定すると、おそらくマイクロ秒精度ではないということです。しかし、重要ではない大多数のアプリケーションのために。

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
を押すだけの時間を割り当て、時間WHの違いを見つける:

あなたはスウィフト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 
} 
関連する問題