0

Interesting task: let say that we've got a counter which counts from 0 to 100. Counting up should start when UIButton was pressed. While UIButton is being pressed/highlighted counting up continue.

Conditions:

  • counter should count up from 0 to 50 in 2 sec
  • counter should count up from 51 to 80 in next 3 sec (from 2 to 5)
  • counter should count up from 81 to 100 in next 5 sec (from 5 to 10)

If I release UIButton counter should start counting down from the last value to 0 (decrement by 1 in 1 sec).

If I press UIButton again counter should start counting up.

user3719188
  • 590
  • 5
  • 7
  • ... and register for touch down and both touch up events on the button to decide when to start your phases of operation. The default wiring to a button to perform an action is touch up inside but `UIControl`s can post every type of control event as per https://developer.apple.com/library/ios/documentation/uikit/reference/uicontrol_class/index.html#//apple_ref/doc/constant_group/Control_Events @RASS I think you should probably promote your comment to an answer, by the way. Then I can move this comment to your answer. – Tommy Nov 28 '14 at 18:53
  • @Tommy, I just promoted it. – Rob Sanders Nov 28 '14 at 19:55

1 Answers1

1

You quite clearly need to write your own algorithms to manage this task. You can use NSTimer to handle the time constraints e.g. [NSTimer scheduledTimerWithTimeInterval:timeInt target:self selector:@selector(myMethod:) userInfo:nil repeats:NO].

I would suggest dividing the time by the number of increments e.g. 2/50 will get you the pause needed between each increment when counting from 0 to 50. Use your natural programming brain and flair for the rest...

Rob Sanders
  • 5,197
  • 3
  • 31
  • 58
  • 1
    Be aware that `NSTimer` may not be accurate enough for such a small interval: http://stackoverflow.com/questions/9737877/how-to-get-a-accurate-timer-in-ios – Michał Ciuba Nov 28 '14 at 22:12