【发布时间】: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)
【问题讨论】:
-
检查这个主题stackoverflow.com/questions/4001043/…它帮助我解决了类似的问题。
-
我在这里发布了答案请检查:stackoverflow.com/a/34741997/4720315
-
@Pipo 你解决问题了吗?
-
@andrey.krukovskiy 对不起,我完全不记得了。这是6年前的事了。对不起!
标签: ios uitableview uiimageview uiimage