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;

@结尾

#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”对象,所以不是那样的。

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

迈克尔

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

好吧,多亏了 Kalle 在下面的建议,这一切都已解决。但是,现在类似的问题再次发生,我想知道我做错了什么。

基本上,我希望在动画的旁边有一颗出现在思想泡泡中的小心脏。我在 BubbleAnimation 类中添加了一个 UIImage,如下所示:

@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 中的一个 UIImageView IBOutlet,名为bubbleHeart。当思想气泡出现时,我使用以下代码显示动画和心脏:

    [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];那么大概我正在重新初始化变量,这样它就不起作用了。知道为什么它没有出现吗?

非常感谢!

迈克尔

4

1 回答 1

0

在您的原始代码中,您从未分配过 的新实例BubbleAnimation,这就是它运行良好的原因。

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

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

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

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

[bubbleAnimation setMood:character.mood];
bubbleAnimation.animationDuration = 0.5; 
bubbleAnimation.animationRepeatCount = 5; 
[bubbleAnimation startAnimating];
于 2010-07-06T09:54:21.600 回答