Reputation: 35
I've done this code to start animation when i click a button, when I click the button is should change what the button says and start the animation, but it only changes what the button says ? Is there anything wrong with my code?
@synthesize SwishImage;
@synthesize SwishButton;
- (IBAction) Swish {
if (SwishImage.isAnimating) {
[SwishImage stopAnimating];
[SwishButton setTitle:@"Swish" forState:UIControlStateNormal];
} else {
[SwishImage startAnimating];
[SwishButton setTitle:@"Searching" forState:UIControlStateNormal];
}
}
- (void)viewdidload
{
NSArray * SwishArray;
SwishArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"Orange_dot.png"],
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
nil];
SwishImage.animationImages=SwishArray;
SwishImage.animationDuration = 1;
SwishImage.animationRepeatCount = 0;
[super viewDidLoad];
}
Upvotes: 0
Views: 85
Reputation: 3447
When I copied this into an empty Xcode project and linked everything correctly in Interface Builder the image view started animating. I assume something isn't correctly linked in Interface Builder.
That being said, your code is a bit awkward from a standards perspective. Variable names should begin with a lower case letter (and that includes properties). You don't need @synthesize anymore and there is a new shortcut for NSArray. Here is how your classes should look:
ViewController.h
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *swishImage;
@property (strong, nonatomic) IBOutlet UIButton *swishButton;
@end
ViewController.m
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *swishArray = @[
[UIImage imageNamed:@"Orange_dot.png"],
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
];
self.swishImage.animationImages=swishArray;
self.swishImage.animationDuration = 1;
self.swishImage.animationRepeatCount = 0;
}
- (IBAction)swish:(id)sender {
if (self.swishImage.isAnimating) {
[self.swishImage stopAnimating];
[self.swishButton setTitle:@"Swish" forState:UIControlStateNormal];
} else {
[self.swishImage startAnimating];
[self.swishButton setTitle:@"Searching" forState:UIControlStateNormal];
}
}
@end
Then just select your image view and go to the Connections Inspector. You should see a section titled Referencing Outlets, make sure that swishImage is connected to your view controller.
Upvotes: 2