ios - 如何在表格 View 单元格的背景中设置图像动画

标签 ios iphone ipad uitableview

如何以编程方式在表格 View 单元格的背景中设置图像?我可以制作动画吗?我可以只为选定的单元格设置动画吗?

注意

我已经使用Answer your own question 功能来回答这个问题。如果您有其他方法,请务必添加答案。 :-)

最佳答案

GIF of Animated cell

要以编程方式将背景图像添加到您的表格 View 单元格(自定义和默认),最简单的方法是在您的 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法。

要设置背景图像,您需要创建 UIImageView 引用,然后将该引用分配给图像。然后为单元格分配 UIImageView 引用。这是在以下代码中完成的,以便在您的表格 View 中的所有单元格中具有默认图像:

//have normal background image
//Create a UIImageView the size of your tableview row. My row (cell) size is 55 and I want it to expand the full length of the window (iPhone). 
//You can create an icon look if you like, just play with the sizing. 
UIImageView * cellBackgrounds = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 55)];

//Next create a reference to the image
UIImage * regularImage = [UIImage imageNamed:@"Image"];

//assign the image to the UIImageView
cellBackgrounds.image = regularImage;

//set the ##background## of the cell
//cell has already been defined when you create the UITableViewCell * cell = ... code
[cell setBackgroundView: cellBackgrounds];

要为单元格设置动画,我们使用相同的方法,只是这次我们引用了一组图像。

首先,我们向项目添加一个文件,其中包含我们将要显示为动画的所有图像。为此,只需在 Xcode 打开时将文件拖放到 Xcode 项目中,然后将其放在包含所有 .h 和 .m 文件的左侧面板中。确保之前按数字顺序命名所有图像 - image1、image2、image3 等。

然后我们使用下面的代码。请注意,唯一的变化是 UIImage 代码:

//have animated background
UIImageView * cellBackgrounds = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 55)];

//Change UIImage imageNamed to UIImage animatedImageNamed and refer to just the name of your images, not the number
//set the duration (which is in seconds) to whatever you like, testing to your desired flow 
UIImage * animatedImages = [UIImage animatedImageNamed:@"image" duration:3.6];
cellBackgrounds.image = animatedImages;
[cell setBackgroundView: cellBackgrounds];

现在你有一个动画表格 View 单元格背景。

如果您只想对选定的行进行动画处理,请执行以下操作:

//Create a new reference to an index path
//I am referencing to the top cell in the first section
NSIndexPath *firstCell = [NSIndexPath indexPathForRow: 0 inSection:0];

//Create a new reference to the UITableViewCell
UITableViewCell *topCell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier forIndexPath:firstCell];
UIImageView * cellBackgrounds = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 55)];
UIImage * animatedImages = [UIImage animatedImageNamed:@"image" duration:3.6];
cellBackgrounds.image = animatedImages;
[cell setBackgroundView: cellBackgrounds];

此新代码将阻止您尝试显示的文本正确显示,因此将其添加到此代码块的底部以确保文本正确显示

topCell.title.text = your reference to Strings to be displayed

享受吧。

关于ios - 如何在表格 View 单元格的背景中设置图像动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26448440/

相关文章:

iphone - iOS:如何将像素从源复制到目标矩形?

iphone - iOS中plist的几个问题

iphone - 对内存管理的一些理解

ios/objective-C : error: linker command failed with exit code 1 (use -v to see invocation)

ios - UIActivityViewController集成中的怪癖

ios - iOS 运行时 PNG 压缩库

iphone - 如何在 XCode 5 中添加 57x57 图标

iphone - 从 iTunes Connect 自动下载销售报告

ios - 如何检测在 iOS 中弹出了哪个 subview Controller ?

iphone - 如何实现像 Instagram 一样的粘性标题功能?