0

I was looking for an indefinite animation technique for my loading animation. The user clicks login, and whilst the JSON stuff is taking care of itself, the spinner spins and then eventually presents a new view controller or login error. I found a great code snippet given by Nate.

The code is given by:

// an ivar for your class:
BOOL animating;

- (void) spinWithOptions: (UIViewAnimationOptions) options {
   // this spin completes 360 degrees every 2 seconds
   [UIView animateWithDuration: 0.5f
                         delay: 0.0f
                       options: options
                    animations: ^{
                       self.imageToMove.transform = CGAffineTransformRotate(imageToMove.transform, M_PI / 2);
                    }
                    completion: ^(BOOL finished) {
                       if (finished) {
                          if (animating) {
                             // if flag still set, keep spinning with constant speed
                             [self spinWithOptions: UIViewAnimationOptionCurveLinear];
                          } else if (options != UIViewAnimationOptionCurveEaseOut) {
                             // one last spin, with deceleration
                             [self spinWithOptions: UIViewAnimationOptionCurveEaseOut];
                          }
                       }
                    }];
}

- (void) startSpin {
   if (!animating) {
      animating = YES;
      [self spinWithOptions: UIViewAnimationOptionCurveEaseIn];
   }   
}

- (void) stopSpin {
    // set the flag to stop spinning after one last 90 degree increment
    animating = NO;
}

When the user clicks 'login' the startSpin method is called, and the JSON stuff is sent. In my JSON post method I have this:

if(success == 1) {
//Present the new view controller
}
else {
[self performSelectorOnMainThread:@selector(stopSpin) withObject:nil waitUntilDone:NO];
[self performSelectorOnMainThread:@selector(hideAnimation) withObject:nil waitUntilDone:NO];

}

This animation method has worked great for an uploading page I use later in my app. However, for this application, it only spins 180 degrees then stops. Then the next page/error eventually loads after a time interval of the inanimate image. Does anybody have any idea as to why this is happening? I dont think it is anything to do with the view controller part because it stops spinning even when the login has failed (no view controller to be presented). I call my startSpin method on the click of a button using:

[self performSelectorOnMainThread:@selector(showAnimation) withObject:nil waitUntilDone:NO];
[self performSelectorOnMainThread:@selector(startSpin) withObject:nil waitUntilDone:NO];

Where show animation is just a method which un-hides the view.

Any thoughts appreciated.

Community
  • 1
  • 1
Michael M
  • 1,034
  • 2
  • 8
  • 21
  • Where, if anywhere, do you call `-stopSpin`? – Tim Sep 05 '13 at 13:11
  • I call stopSpin when the success = 0 - Ill add that in now. I have an NSLog(@"Stop spin called") line in my stopSpin method, and at no point does this get called when the login has been successful and the new controller is being presented. – Michael M Sep 05 '13 at 13:12
  • OK, thanks. You say "In my JSON post method I have this..." - is that in the method to *send* the JSON query, or the method/handler/callback that *receives* a response? If you're checking success on sending, you may be doing it too early, since you're still waiting for a JSON response to come back. – Tim Sep 05 '13 at 13:14
  • Nope, all the stopSpin methods are based on a success value which is derived from the response of the JSON request. The handling of the response is done within the same method that sends the data. So: NSHTTPURLResponse *response = nil; NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; //NSLog(@"Response code: %d", [response statusCode]); NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; (Sorry if its cluttered). Success is then derived from that response data – Michael M Sep 05 '13 at 13:18

1 Answers1

0

Basically you set the transform to rotate to Pi/2, 180 degrees, and the second round you set it to the same value. Setting something to the same value as before will not animate it. You neef to find a way to add another 180 degrees to the rotation transform.

Rob van der Veer
  • 1,148
  • 1
  • 7
  • 20