Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 916499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:03:46+00:00 2026-05-15T18:03:46+00:00

I’m trying to subclass UIImageView in order to create an instance of the subclass

  • 0

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. ‘bubbleAnimationTemp’ is just a UIImageView object I declared in the header:

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:

#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.

Any help as to why nothing appears would be very much appreciated. Thanks as always!

Michael

****************UPDATE****************

Well, thanks to Kalle’s advice below, this is all fixed. However, now a similar issue is reoccurring and I wonder what I’m doing wrong.

Basically, I want to have a small heart that appears in the thought bubble, alongside the animation. I’ve added a UIImage to the BubbleAnimation class like so:

@interface BubbleAnimation : UIImageView {
    UIImage     *emotAnim1;
    UIImage     *emotAnim2;
    UIImage     *heart;
}

@property (nonatomic, retain) UIImage *heart;

- (void)setMood:(NSString *)mood;

@end

And synthesise it in the implementation as usual. Then I set the heart to the correct colour in the setMood method:

- (void)setMood:(NSString *)mood {
    if ([mood isEqualToString:kGood]) {
        emotAnim1 = [UIImage imageNamed:@"Good1.png"];
        emotAnim2 = [UIImage imageNamed:@"Good2.png"];
        self.heart = [UIImage imageNamed:@"HeartRed.png"];
    }
    else if ...

In IB, I’ve added a hidden UIImageView object and linked it to a UIImageView IBOutlet in my ViewController called bubbleHeart. When the thought bubble appears, I use the following code to display the animations and the heart:

    [bubbleAnimation setMood:charachter.mood];
self.bubbleHeart.image = bubbleAnimation.heart;
        bubbleAnimation.animationDuration = kAnimationDuration;
        bubbleAnimation.animationRepeatCount = kAnimationRepeatCount;
        [bubbleAnimation startAnimating];
        bubbleHeart.hidden = FALSE;

The problem is, the animation appears, but the little heart doesn’t. I’ve tried various approaches – I created the UIImageView in the BubbleAnimation class, instead of using a UIImage, and tried initialising it in various different ways, but no joy. If I call something like self.bubbleHeart = [[UIImageView alloc] initWithImage:bubbleAnimation.heart]; then presumably I’m reinitialising the variable so that doesn’t work. Any idea why it’s not appearing?

Many thanks!

Michael

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:03:47+00:00Added an answer on May 15, 2026 at 6:03 pm

    In your original code, you never allocated a new instance of BubbleAnimation, and that’s why it worked fine.

    You have an object in the XIB file which is the BubbleAnimation object, and that’s all fine, but the problem is that you’re allocating a new instance of BubbleAnimation instead of using the one you have in your revised code.

    Even if you were to use the one you have, you’d have to change the init method, since the system will call initWithCoder:, not initWithMood:.

    The best thing is most likely to change the init function to something else, e.g. a setMood:, which modifies the image view. Then you re-use the same image view every time.

    Then you’d just set it up using something like…

    [bubbleAnimation setMood:character.mood];
    bubbleAnimation.animationDuration = 0.5; 
    bubbleAnimation.animationRepeatCount = 5; 
    [bubbleAnimation startAnimating];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.