0

I need to have a button move using a UIView animation when the view loads. A nearly identical animation works in another view when a button is pushed. This is my ViewDidLoad

HTMLButton.hidden = NO;
HTMLButton.enabled = NO;

[UIView animateWithDuration:1.0
                 animations:^{
                     CGRect HTMLRect = HTMLButton.frame;
                     HTMLRect.origin = CGPointMake(HTMLRect.origin.x, HTMLRect.origin.y + 70);
                     HTMLButton.frame = HTMLRect;
                     NSLog(@"Animation Called");
                 }];

The NSLog does compile but no animation at all. Can I not call the animation here or am I doing something wrong?

Thank you to anyone who can help

Edit: I used the same Animation called from a separate IBAction in a different button, the animation still doesnt work

3
  • Have you tried putting this code in viewDidAppear instead of viewDidLoad?
    – Isaac
    Apr 27, 2013 at 19:40
  • Have you checked that HTMLButton is actually set to something and not nil?
    – Isaac
    Apr 27, 2013 at 19:47
  • HTMLButton is attached to a button in my .nib file but it is not nil. Apr 27, 2013 at 19:51

1 Answer 1

0

viewDidLoad or viewDidAppear internals might be interfering with the animation. There might be other tasks in viewDidLoad so your button is not even fully loaded when you call frame change animation. I'm not sure if this will solve your problem but if it happened to me I would definitely give this a try:

double delayInSeconds = 0.2; //Or you may play with it further
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

    [UIView animateWithDuration:1.0
             animations:^{
                 CGRect HTMLRect = HTMLButton.frame;
                 HTMLRect.origin = CGPointMake(HTMLRect.origin.x, HTMLRect.origin.y + 70);
                 HTMLButton.frame = HTMLRect;
                 NSLog(@"Animation Called");
             }];

});

This will delay the animation call a little (it is not possible for users to understand 0.2 sec lag) so your viewcontroller is in a waiting state rather than doing any initialization.

4
  • put an extreme delay then like 5 secs. if it is still not animating then you should look for other problems with your button init.
    – guenis
    Apr 27, 2013 at 20:11
  • I have no idea why this worked but I removed HTMLButton = [[UIButton alloc] init]; from my .m and it works. Your last comment is what made me look into the initialization of it so thank you. Apr 27, 2013 at 20:57
  • 1
    In the comments you wrote the button is linked in the .nib file. You don't re-alloc controls that are defined in the .nib or storyboard, that's not correct. They are already allocated and init'ed when nibs are loaded.
    – guenis
    Apr 27, 2013 at 21:25
  • Im used to initializing everything from when I learned Java, it was just a force of habbit Apr 28, 2013 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.