UIButton Animation not working

863 views Asked by At

I am new at xcode and I am learning something new each day, I have gotten stuck. I want to create a custom button on a second Storyboard (MapTwoViewController), lets say it is a custom button that looks like a cloud and it goes from left to right.

I have created the custom button in the storyboard and I have created the IBOutlet in the GWSMapTwoViewController.h so it looks like this and is linked to the button:

#import <UIKit/UIKit.h>

@interface GWSMapTwoViewController : UIViewController
@property (nonatomic, strong) IBOutlet UIButton *cloudAnimate;

@end

So in my GWSMapTwoViewController.m in my view did load I have the following:

#import "GWSMapTwoViewController.h"

@interface GWSMapTwoViewController ()

@end

@implementation GWSMapTwoViewController
@synthesize cloudAnimate;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Move UIView

    cloudAnimate.center = CGPointMake(200.0, 100.0);

    [UIView animateWithDuration:2.0 animations:^{ cloudAnimate.center = CGPointMake(100.0, 200.0);}];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

It just doesn't want to animate, it just goes straight to the end point, if I remove the animation code then the cloud appears at 200, 100 but if I put the animate code in it just moves to 100, 200 or whatever coordinates i put in. It just doesn't animate so its either in the starting point or the end point.

Can anyone tell when what i am doing wrong?

1

There are 1 answers

3
Dimentar On BEST ANSWER

Move your animation code in

 - (void)viewDidAppear:(BOOL)animated

because viewDidLoad is called before the view is visible.

Don't forget to call super