【问题标题】:UIImageView in UITableViewCell not animating when an animated UIImage is assigned twice当动画 UIImage 被分配两次时,UITableViewCell 中的 UIImageView 没有动画
【发布时间】:2013-05-12 13:10:36
【问题描述】:

我正在尝试在 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

这是我点击单元格两次时的输出:

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)

【问题讨论】:

标签: ios uitableview uiimageview uiimage


【解决方案1】:

我强烈建议您不要对cellForRowAtIndexPath 中的图像进行任何动画处理,因为如果您有更多图像,那么当您快速滚动时会出现不良效果(单元格图像会因单元格可重用性而切换)。

在我的一个项目中,我为 UITableViewDelegate(符合 UIScrollViewDelegate)实现了 – scrollViewDidScroll: 方法,并在那里调用了一种方法,该方法仅在可见单元格(tableView.visibleCells)上为图像设置动画。

另一方面,我不明白您为什么使用 static BOOL second = NO;,您可以简单地检查 cell.imageView.image == nil 与否。

还要检查您的动画代码,有可能某些地方无法正常工作(请记住,单元格被重复使用) .

【讨论】:

  • - scrollViewDidScroll: 每次 tableView 滚动几个像素时都会被调用。你的意思是- scrollViewDidEndDecelerating:?我无法检查 (cell.imageView.image == nil),因为 [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 并不总是返回相同的 UITableViewCell。
  • -scrollViewDidScroll 中检查可见单元格是可以的,因为如果您已经进行了适当的更改,图像将不再更改。您也可以在-scrollViewDidEndDecelrating: 中更改它,但行为不会像在-scrollViewDidScroll 中那样酷
  • 但是- scrollViewDidScroll:- tableView:didSelectRowAtIndexPath:之后没有被调用。因此,如果用户不滚动,图像将不会被动画化。在进一步测试之后,我确定问题与在调用- tableView:didSelectRowAtIndexPath: 之前显示的 UITableViewCell 的动画有关。 UIActivityIndi​​catorView 可以工作,但我认为它不是 UIAnimatedImage。
猜你喜欢
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多