Hi everyone,
I'm trying to subclass UIImageView in order to create an instance of the subclass which plays different animations, depending on a variable I send it. My initial code (before subclassing) for playing a specific 2 frame animation looked like this (and worked fine). 'bubbleAnimationTemp' is just a UIImageView object I declared in the header:
Code:
UIImage *animImage1 = [UIImage imageNamed:@"HappyAnim1.png"];
UIImage *animImage2 = [UIImage imageNamed:@"HappyAnim2.png"];
NSArray *images = [[NSArray alloc] initWithObjects:animImage1, animImage2, nil];
bubbleAnimationTemp.animationImages = images;
[images release];
bubbleAnimationTemp.animationDuration = 0.5;
bubbleAnimationTemp.animationRepeatCount = 5;
[bubbleAnimationTemp startAnimating];
So then I tried subclassing UIImageView like so:
Code:
#import <UIKit/UIKit.h>
#import "Interaction.h"
@interface BubbleAnimation : UIImageView {
UIImage *emotAnim1;
UIImage *emotAnim2;
}
@property (nonatomic, retain) UIImage *emotAnim1;
@property (nonatomic, retain) UIImage *emotAnim2;
- (BubbleAnimation *)initWithMood:(NSString *)mood;
@end
#import "BubbleAnimation.h"
@implementation BubbleAnimation
@synthesize emotAnim1;
@synthesize emotAnim2;
- (BubbleAnimation *)initWithMood:(NSString *)mood {
if (self = [super init]) {
NSLog(@"Mood: %@", mood);
if ([mood isEqualToString:kGood]) {
emotAnim1 = [[UIImage alloc] initWithContentsOfFile:([[NSBundle mainBundle] pathForResource:@"HappyAnim1" ofType:@"png"])];
emotAnim2 = [[UIImage alloc] initWithContentsOfFile:([[NSBundle mainBundle] pathForResource:@"HappyAnim2" ofType:@"png"])];
//emotAnim1 = [UIImage imageNamed:@"HappyAnim1.png"];
//emotAnim2 = [UIImage imageNamed:@"HappyAnim2.png"];
}
else if ([mood isEqualToString:kNeutral]) {
emotAnim1 = [UIImage imageNamed:@"NeutralAnim1.png"];
emotAnim2 = [UIImage imageNamed:@"NeutralAnim2.png"];
}
else {
emotAnim1 = [UIImage imageNamed:@"SadAnim1.png"];
emotAnim2 = [UIImage imageNamed:@"SadAnim2.png"];
}
NSArray *images = [[NSArray alloc] initWithObjects:emotAnim1, emotAnim2, nil];
self.animationImages = images;
[images release];
}
return self;
}
As you can see, I tried two different approaches for creating the UIImages to add to the UIImageView. But the problem I'm having is that nothing shows up when the animation plays.
I also tried simply copying the code from the first method into this subclass, so the process is essentially the same, but still nothing appears.
I've checked the documentation for notes on subclassing UIImageView but there doesn't seem to be anything I'm missing. I've made sure to change the 'UIImageView' object I placed in Interface Builder into a 'BubbleAnimation' object, so it's not that.
By the way, this is how I'm calling the animation to be played:
Code:
bubbleAnimation = [[BubbleAnimation alloc] initWithMood:charcter.mood];
bubbleAnimation.animationDuration = 0.5;
bubbleAnimation.animationRepeatCount = 5;
[bubbleAnimation startAnimating];
Any help as to why nothing appears would be very much appreciated. Thanks as always!
Michael