gpt4 book ai didi

ios - 当动画 UIImage 被分配两次时,UITableViewCell 中的 UIImageView 没有动画

转载 作者:可可西里 更新时间:2023-11-01 04:53:20 24 4
gpt4 key购买 nike

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

最佳答案

我强烈建议你不要对 cellForRowAtIndexPath 中的图像做任何动画,因为如果你有更多的图像,那么当你快速滚动时会出现不希望的效果(单元格图像会切换由于单元可重用性)。

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

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

同时检查您的动画代码,可能那里的某些东西无法正常工作(请记住,单元格被重用).

关于ios - 当动画 UIImage 被分配两次时,UITableViewCell 中的 UIImageView 没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16507718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com