如何在表格视图单元格背景中设置图像动画
问题描述:
如何以编程方式在表格视图单元格背景中设置图像?我可以制作动画吗?我可以只动画一个选定的单元格吗?如何在表格视图单元格背景中设置图像动画
注意
我已经使用了Answer your own question
功能来回答这个问题。如果您有其他方法,请通过一切手段添加答案。 :-)
答
背景图像编程方式添加到您的表格视图单元格(定制和默认)它是在你- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
最简单的方法完成。
要设置背景图像,您需要创建UIImageView引用,然后将该引用分配给图像。然后将单元格分配给UIImageView引用。这在下面的代码做有一个默认的图像在所有细胞中的实现代码如下:
//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];
现在你有一个动画表视图单元格的背景。
如果你只是想在这里动画选定行是你如何做到这一点:
//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
享受。
答
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//1. Setup the CATransform3D structure
CATransform3D rotation;
rotation = CATransform3DMakeRotation((90.0*M_PI)/180, 0.0, 0.7, 0.4);
rotation.m34 = 1.0/ -600;
//2. Define the initial state (Before the animation)
cell.layer.shadowColor = [[UIColor blackColor]CGColor];
cell.layer.shadowOffset = CGSizeMake(10, 10);
cell.alpha = 0;
cell.layer.transform = rotation;
cell.layer.anchorPoint = CGPointMake(0, 0.5);
//3. Define the final state (After the animation) and commit the animation
[UIView beginAnimations:@"rotation" context:NULL];
[UIView setAnimationDuration:0.8];
cell.layer.transform = CATransform3DIdentity;
cell.alpha = 1;
cell.layer.shadowOffset = CGSizeMake(0, 0);
[UIView commitAnimations];
}
所有真正的,除非你使用的出队功能,节省内存,你真的应该做的'willDisplayCell'中的大部分。 – tooluser 2014-10-20 15:46:50
可能最好是获得屏幕宽度的界限并在更宽的屏幕上正确显示。 – 2015-06-27 09:00:13