0

I wrote this line:

NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "showGallery:", userInfo: nil, repeats: false)

So, it runs when my 0.5 seconds are expired. But I want to run those function if there are difference between seconds is less than 0.5.

For example: I touch the screen and do not release it 1 second. Then it do not need to run my func, but if I touched and released quickly, then it must run.

How can I modify my function?

John
  • 183
  • 1
  • 3
  • 11
  • 1
    Just in case you actually want to implement the touch example that you described, you'll be better off using `UILongPressGestureRecognizer` or `UITapRecognizer`, or a combination of both (requiring one to fail). `NSTimer` is not very accurate: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/#//apple_ref/occ/instp/NSTimer/tolerance. – ldiqual Jun 23 '16 at 08:04

2 Answers2

1

Measure the elapsed time when the press is released and execute the function if the interval is too short. As is, you're going about it wrong with a timer

Feldur
  • 1,121
  • 9
  • 23
0

As @Feldur said, you have to change your approach with the timer. Try to implement something like stopwatch from this question.

Start timer with 1/10 of sec as interval when touch begins. In fire method count how many times timer did fire. If it fired too many times - invalidate timer. When touch ends invalidate timer and check how many times it fired. If it didn't fire enough times - run method you wanted.

Community
  • 1
  • 1
tahavath
  • 234
  • 2
  • 9