1

の単純な 2 フレーム アニメーションを実行しようとしていUIButtonます。プロパティを使用してこれが可能であることを理解していUIButton's imageView.animationImagesます-プロパティを配列に設定してアニメーション化します。しかし、そうすると何も表示されません。参考までに、buttonImages 配列を確認しましたが、実際に表示したい画像が含まれています。私が間違っているアイデアはありますか?

NSArray *buttonImages = bookDoc.book.buttonImages;
UIImage *bookImage = bookDoc.thumbImage;
UIButton *bookButton = [UIButton buttonWithType:UIButtonTypeCustom];
bookButton.frame = CGRectMake(0, 0, bookImage.size.width, bookImage.size.height);
//[bookButton setBackgroundImage:bookImage forState:UIControlStateNormal];
bookButton.imageView.animationImages = buttonImages;
bookButton.imageView.animationDuration = 0.5;
bookButton.imageView.animationRepeatCount = INFINITY;
[bookButton addTarget:self action:@selector(bookButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[bookButton.imageView startAnimating];
4

3 に答える 3

3

imageViewUIButton(0,0,0,0) のフレームで初期化されます。つまり、表示されません (実際には非表示にもなります)。

frameおよびhiddenプロパティを自分で構成できます。

bookButton.imageView.frame = CGRectMake(0, 0, 20, 20);
bookButton.imageView.hidden = NO;

または、さらに単純に、アニメーションの最初の画像を使用するだけです

[bookButton setImage:buttonImages[0] forState:UIControlStateNormal];

これにより、imageView が再表示され、正しいフレームが設定されます。

imageView が表示されると、アニメーション化されます。

于 2013-01-09T12:10:16.640 に答える
1

ボタンの imageView プロパティに画像を保存しようとしたことはないと思います。私はいつも setImage:forState または setBackgroundImage:forState を使用してきました。

私の推測では、ボタンはデフォルトの画像状態に (nil) 画像を使用して、画像ビュー自体の画像プロパティを設定していると思います。

button.imageView.image プロパティを静的画像に設定して、それが機能するかどうかを確認しましたか? 私の推測では、私が言ったように、ボタンは setImage:forState で提供された設定で提供された画像を上書きします (または、現在の状態の画像を提供したことがない場合は nil です)。

于 2012-05-17T19:21:46.390 に答える
0

私はちょうどこれを書いた、それは動作します。その欠点は、IB で設定した強調表示された画像が機能していないように見えることです。Xcode 5.無効化された画像なども機能しない可能性があります。あなたがそれを気にしないなら、これはうまくいきます。

+(void)animatemjad:(UIButton*)button offimagenames:(NSArray*)imagenames startdelay:(const NSTimeInterval)STARTDELAY {
    UIImageView* imageview = [button imageView];
    NSMutableArray* images = [NSMutableArray array];
    for (NSString* imagename in imagenames) {
        UIImage* image = [UIImage imageNamed:imagename];
        NSAssert(image != nil, @"Failed loading imagename: %@", imagename);
        if (image == nil)
            return; // return out here... whatever
        [images addObject:image];
    }
    imageview = [[UIImageView alloc] initWithImage:images.firstObject];
    [button addSubview:imageview];
    imageview.animationImages = images;
    imageview.animationDuration = 5;
    imageview.animationRepeatCount = 0;
    [imageview performSelector:@selector(startAnimating) withObject:nil afterDelay:STARTDELAY]; // [imageview startAnimating];
}
于 2014-07-18T02:23:13.390 に答える