如何在表格视图单元格的背景中设置图像动画 [英] How to set an animation of images in background of table view cell

查看:126
本文介绍了如何在表格视图单元格的背景中设置图像动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I programmatically set an image in the background of a table view cell? Can I animate it? Can I animate just a selected cell?

注意

我使用过回答你的问题自己的问题功能来回答这个问题。如果您有其他方法,请务必添加答案。 : - )

I have used the Answer your own question feature to answer this question. Please, by all means add an answer if you've got other methods. :-)

推荐答案

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

To programatically add a background image to your table view cell (both custom and default) it is easiest done in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method.

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

To set the background image, you need to create UIImageView reference, then assign that reference to an image. Then assign cell the UIImageView reference. This is done in the following code to have a default image throughout all cells in your tableview:

//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];

为了给单元设置动画,我们使用相同的方法,除非这次我们引用一组图像。

To animate the cell we do the same method except this time we refer to a set of images.

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

First we add a file to the project containing all the images we are going to display as an animation. To do this, simply drag the file into your Xcode project while Xcode is open and drop it in the left panel that contains all your .h and .m files. Make sure to name all your images in numerical order - image1, image2, image3, etc - before.

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

Then we use the following code. Notice the only change is the UIImage code:

//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];

现在你有了一个动画表格视图单元格背景。

Now you have an animated table view cell background.

如果你只想在这里动画一个选定的行,你就会这样做:

If you only want a selected row to be animated here's how you do it:

//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];

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

This new code will prevent the text you're trying to display from displaying properly so add this to bottom of this block of code to ensure the text displays properly

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

享受。

这篇关于如何在表格视图单元格的背景中设置图像动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆