0

What I want to do is call a method after a delay for a game which will reload a weapon, but if I do that and the user pauses the game that selector isn't getting paused so the user can cheat by pausing.

So Im wondering if there is a way to pause it and then continue.

I know how to cancel the selector with this: cancelPreviousPerformRequestsWithTarget: but I want it to be able to continue after the user resumes the game.

Is this the right approach or should I consider another strategy?

Arbitur
  • 38,684
  • 22
  • 91
  • 128
  • Maybe you can use something like http://stackoverflow.com/a/11984865/513286 – cekisakurek Jul 17 '14 at 12:15
  • What I can think of the top of my head is to create an object of a custom class which has an NSTimer which calls a function in that class several times until it reaches its' final number of fires and then call a selector to reload. But that seems CPU heavy. – Arbitur Jul 17 '14 at 12:20
  • Do you have any kind of overall timer for your game, which increments and drives changes the the game state each time it fires? – Wain Jul 17 '14 at 12:29
  • I have a main NSTimer which fires the game updater so everything moves correctly and smoothly. But i dont think that is a very good idead to use the main timer for what youre supposing. – Arbitur Jul 17 '14 at 12:39

1 Answers1

0

This is absolutely not a good way to solve this problem, but it did the trick for me. I've got a scaling animation, where a view grows to scale 6.0, waits for 2 seconds and then scales back to 1.0. The wait for 2 seconds is my part where I used the performSelector:withObject:afterDelay: which I couldn't pause.

This is the pause part:

[UIView animateWithDuration:0 delay:2.0 options:0 animations:^{
    self.transform = CGAffineTransformMakeScale(6.0 + 0.001, 6.0 + 0.001);
} completion:^(BOOL finished) {
    [self animateOutWithCompletion:completion];
}];

Make sure you are animation a change, thats why I add 0.001 (which you wont see, as it's a that little change).

UIView animations can be paused using this category:

@interface UIView (AnimationsHandler)

- (void)pauseAnimations;
- (void)resumeAnimations;

@end

@implementation UIView (AnimationsHandler)
- (void)pauseAnimations
{
    CFTimeInterval paused_time = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    self.layer.speed = 0.0;
    self.layer.timeOffset = paused_time;
}

- (void)resumeAnimations
{
    CFTimeInterval paused_time = [self.layer timeOffset];
    self.layer.speed = 1.0f;
    self.layer.timeOffset = 0.0f;
    self.layer.beginTime = 0.0f;
    CFTimeInterval time_since_pause = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil] - paused_time;
    self.layer.beginTime = time_since_pause;
}

Feel free to use this code, but as I said before, this is not a good way to solve this problem. It's just making use of the way UIView animations work and the possibility to pause these animations.

Antoine
  • 23,526
  • 11
  • 88
  • 94