2014-10-19 445 views
1

如何以編程方式在表格視圖單元格背景中設置圖像?我可以製作動畫嗎?我可以只動畫一個選定的單元格嗎?如何在表格視圖單元格背景中設置圖像動畫

注意

我已經使用了Answer your own question功能來回答這個問題。如果您有其他方法,請通過一切手段添加答案。 :-)

回答

1

GIF of Animated cell

背景圖像編程方式添加到您的表格視圖單元格(定製和默認)它是在你- (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 

享受。

+0

所有真正的,除非你使用的出隊功能,節省內存,你真的應該做的'willDisplayCell'中的大部分。 – tooluser 2014-10-20 15:46:50

+0

可能最好是獲得屏幕寬度的界限並在更寬的屏幕上正確顯示。 – 2015-06-27 09:00:13

0
-(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]; 

} 
相關問題