5

UITableViewCell の imageView にアニメーション化された UIImage を表示しようとしています。アニメーション イメージは、最初の割り当ての後に表示されますが、それ以降のすべての試行では表示されません。

TableViewController のコードは次のとおりです。

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIImage *animatedImage;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.animatedImage = [UIImage animatedImageNamed:@"syncing" duration:1.0];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static BOOL second = NO;

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (second) {

        cell.imageView.image = nil;
        NSLog(@"Removed image");
    }
    else {

        cell.imageView.image = self.animatedImage;
        NSLog(@"Added image");

        if (![cell.imageView isAnimating]) {

            [cell.imageView startAnimating];
            NSLog(@"Started animation for UIImageView: %@", cell.imageView);
        }
    }

    second = !second;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

@end

これは、セルを 2 回タップしたときの出力です。

Added image
Started animation for UIImageView: <UIImageView: 0x7197540; frame = (0 0; 0 0); opaque = NO; userInteractionEnabled = NO; animations = { UIImageAnimation=<CAKeyframeAnimation: 0x715bc80>; }; layer = <CALayer: 0x71975a0>> - (null)
Removed image
Added image
Started animation for UIImageView: <UIImageView: 0x7197540; frame = (6 6; 30 30); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x71975a0>> - (null)
4

1 に答える 1

1

画像にアニメーションを付けないことを強くお勧めします。画像cellForRowAtIndexPathが多い場合、高速スクロールすると望ましくない効果が現れるためです (セルの再利用性のためにセル画像が切り替わります)。

私のプロジェクトの 1 つで行ったことは– scrollViewDidScroll:、UITableViewDelegate (UIScrollViewDelegate に準拠) のメソッドを実装し、そこで可視セル (tableView.visibleCells) でのみ画像をアニメーション化するメソッドを呼び出しました。

一方、static BOOL second = NO;cell.imageView.image == nil であるかどうかを単純に確認できる を使用している理由がわかりません。

また、アニメーション コードを確認してください。何かが適切に機能していない可能性があります (セルが再利用されていることに注意してください)。

于 2013-05-12T17:31:03.747 に答える