子类化 UIImageView - 动画图像不显示或出现

发布于 2024-09-08 16:29:52 字数 3998 浏览 4 评论 0原文

我正在尝试对 UIImageView 进行子类化,以便创建一个播放不同动画的子类实例,具体取决于我发送的变量。我用于播放特定 2 帧动画的初始代码(在子类化之前)如下所示。 'bubbleAnimationTemp' 只是我在标头中声明的 UIImageView 对象:

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];

然后我尝试像这样子类化 UIImageView:

#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;
}

如您所见,我尝试了两种不同的方法来创建 UIImages 以添加到 UIImageView 中。但我遇到的问题是动画播放时什么也没有显示。

我也尝试简单地将代码从第一个方法复制到这个子类中,因此过程本质上是相同的,但仍然没有出现任何内容。

我已经检查了文档中有关 UIImageView 子类化的注释,但似乎没有遗漏任何内容。我已确保将放置在 Interface Builder 中的“UIImageView”对象更改为“BubbleAnimation”对象,所以事实并非如此。

任何关于为什么什么都没有出现的帮助将非常感激。一如既往的感谢!

迈克尔

****************更新****************

好吧,感谢下面卡勒的建议,这一切都已解决。然而,现在类似的问题再次出现,我想知道我做错了什么。

基本上,我想要一颗小心出现在动画旁边的思想泡泡中。我已经将 UIImage 添加到 BubbleAnimation 类中,如下所示:

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

@property (nonatomic, retain) UIImage *heart;

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

@end

并像往常一样在实现中综合它。然后,我在 setMood 方法中将心形设置为正确的颜色:

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

在 IB 中,我添加了一个隐藏的 UIImageView 对象,并将其链接到我的 ViewController 中名为 bubbleHeart 的 UIImageView IBOutlet。当思想泡泡出现时,我使用下面的代码来显示动画和心形:

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

问题是,动画出现了,但小心形却没有。我尝试了各种方法 - 我在 BubbleAnimation 类中创建了 UIImageView,而不是使用 UIImage,并尝试以各种不同的方式初始化它,但没有乐趣。如果我调用类似 self.bubbleHeart = [[UIImageView alloc] initWithImage:bubbleAnimation.heart]; 的内容,那么我可能会重新初始化变量,这样就不起作用了。知道为什么它没有出现吗?

非常感谢!

迈克尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒带 2024-09-15 16:29:52

在您的原始代码中,您从未分配过 BubbleAnimation 的新实例,这就是它工作正常的原因。

XIB 文件中有一个对象,即 BubbleAnimation 对象,这一切都很好,但问题是您正在分配 BubbleAnimation 的新实例,而不是使用修改后的代码中的实例。

即使您要使用已有的方法,也必须更改 init 方法,因为系统将调用 initWithCoder:,而不是 initWithMood:

最好的办法就是将 init 函数更改为其他函数,例如 setMood:,它会修改图像视图。然后您每次都重复使用相同的图像视图。

然后你只需使用类似的东西来设置它......

[bubbleAnimation setMood:character.mood];
bubbleAnimation.animationDuration = 0.5; 
bubbleAnimation.animationRepeatCount = 5; 
[bubbleAnimation startAnimating];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文