Animation not Running When Method is Called.

Discussion in 'iOS Programming' started by gwelmarten, Jul 6, 2012.

  1. gwelmarten macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #1
    Hi
    I am trying to animate a button to change it's shape and position (CGRect) using cocoa animation. I make the button using one method, and then call the method to animate the button. This method is called (an NSLog runs to tell me this) however the animation does not take place.
    I've reviewed my code copious times, and referred to my reference texts, but just can't figure out why it's not working. If anybody can figure it out, I'd be just so grateful.

    In my implementation file, I have:

    Code:
    @implementation ViewController
    
    UIButton *button;
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	// Do any additional setup after loading the view, typically from a nib.
        [self makeButton];
        [self moveButton:button];
    
    }
    
    - (void)makeButton {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button addTarget:self 
                   action:@selector(methodToCallWhenButtonPressed)
         forControlEvents:UIControlEventTouchDown];
        [button setTitle:@"Show View" forState:UIControlStateNormal];
        button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
        [self.view addSubview:button];
    }
    
    - (void)moveButton:(UIButton *)buttonToUse {
        NSLog(@"Called");
        [UIView animateWithDuration:0.5 delay:0.0 options:nil animations:^{
            buttonToUse.frame = CGRectMake(10, 10, 300, 440);
        } completion:^(BOOL finished){
            
        }];
    }
    and in my header file, I have:
    Code:
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    - (void) makeButton;
    - (void) moveButton:(UIButton *)buttonToUse;
    @end
    
    Any ideas?

    Thanks,
    Sam
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What's the value of buttonToUse when you call moveButton:?
     
  3. gwelmarten thread starter macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #3
    Hi
    See the viewDidLoad method:
    I call: [self moveButton:button];
    So, in this method I am referring to the UIButton called 'button'.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    I know. And I'm pretty sure the value is nil when you make that call. But do you understand why?
     
  5. gwelmarten thread starter macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #5
    Hi
    Thanks for responding.
    No, I don't understand why, which I find odd bearing in mind the amount of Obj-C I have done in the 2 years. I'm sure it's just something little I'm missing.

    Obviously, there is nothing wrong with the animation code (if I move it up to the makeButton method, everything works). Also, if I remove the declaration in the title of the moveButton method, to get:
    Code:
    -(void)moveButton {
    code
    }
    and then I call
    Code:
    [self moveButton];
    that doesn't work. I'm not sure why, bearing in mind I declared the UIButton with it's name at the top of the implementation file.

    Sam
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Do you see anywhere else where you've declared/assigned a UIButton *button?
     
  7. gwelmarten thread starter macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #7
    Ah, of course! Two declarations of one object type with the same name!
    Ok - so that's fixed it. I just removed the section that declares it for the second time.
    Probably because I added the declaration at the top at a later point.
    Would you mind possibly explaining to me why that happens? Is it as simple as you can't have two objects called the same thing (e.g. two files in a folder on your Mac), or is it more complex?

    Thanks for your help - kicking myself now!

    Sam
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    More complex. It all has to do with scope.
     

Share This Page