Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

gwelmarten

macrumors 6502
Original poster
Jan 17, 2011
476
0
England!
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
 
I know. And I'm pretty sure the value is nil when you make that call. But do you understand why?

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
 
Do you see anywhere else where you've declared/assigned a UIButton *button?

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.