今天有點(diǎn)空余時(shí)間,看下別人寫的一個(gè)表視圖下拉效果實(shí)現(xiàn)伸長(zhǎng)/收縮效果 實(shí)現(xiàn)效果圖如下:

表視圖伸長(zhǎng)收縮效果.gif
具體代碼如下,實(shí)現(xiàn)的功能簡(jiǎn)單。
這兩個(gè)方法,是配合起來使用的,標(biāo)記了一個(gè)tableView的動(dòng)畫塊。\n分別代表動(dòng)畫的開始開始和結(jié)束。兩者成對(duì)出現(xiàn),可以嵌套使用。一般,在添加,刪除,選擇 tableView中使用,并實(shí)現(xiàn)動(dòng)畫效果。\n在動(dòng)畫塊內(nèi),不建議使用reloadData方法,如果使用,會(huì)影響動(dòng)畫。
[self.tableView beginUpdates];
[self.tableView endUpdates];
代碼如下:
#import "ViewController.h"
#import "XPViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
/**正常的cell*/
@property (nonatomic, strong) XPViewCell *normalCell;
/**選中的cell*/
@property (nonatomic, strong) XPViewCell *selectedCell;
/**重復(fù)點(diǎn)擊標(biāo)識(shí)*/
@property (nonatomic, assign) BOOL repeatClick;
/**保存選中行*/
@property (nonatomic, assign) NSInteger tmpRow;
/**接受選中行*/
@property (nonatomic, assign) NSInteger selectRow;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.repeatClick = YES;
self.tmpRow = 200;
self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == self.tmpRow && self.repeatClick == YES) {
return 230;
}else{
return 70;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
_normalCell = [tableView dequeueReusableCellWithIdentifier:@"normalCell"];
if (!_normalCell) {
_normalCell = [[XPViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"normalCell"];
_normalCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (indexPath.row == 0) {
_normalCell.backgroundColor = [UIColor redColor];
}else if (indexPath.row == 1){
_normalCell.backgroundColor = [UIColor cyanColor];
}else if (indexPath.row == 2){
_normalCell.backgroundColor = [UIColor greenColor];
}else{
_normalCell.backgroundColor = [UIColor magentaColor];
}
return _normalCell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.tmpRow != 200) {
//相同的Cell
if (self.tmpRow == indexPath.row) {
self.repeatClick = !self.repeatClick;
if (self.repeatClick == YES) {
[self.tableView beginUpdates];
self.selectedCell = (XPViewCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.tmpRow inSection:0]];
[self stretchAnimationIndexForRow:self.tmpRow tableViewCell:self.selectedCell];
[self.tableView endUpdates];
}else{
[self.tableView beginUpdates];
self.selectedCell = (XPViewCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.tmpRow inSection:0]];
[self contractAnimationIndexForRow:self.tmpRow tableViewCell:self.selectedCell];
[self.tableView endUpdates];
}
}else{
//不同的Cell
if (self.repeatClick == NO) {
[self.tableView beginUpdates];
self.selectedCell = (XPViewCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.tmpRow inSection:0]];
[self contractAnimationIndexForRow:self.tmpRow tableViewCell:self.selectedCell];
[self.tableView endUpdates];
}
[self.tableView beginUpdates];
self.repeatClick = YES;
self.tmpRow = indexPath.row;
self.selectedCell = (XPViewCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.tmpRow inSection:0]];
[self stretchAnimationIndexForRow:self.tmpRow tableViewCell:self.selectedCell];
[self.tableView endUpdates];
}
}else{
[self.tableView beginUpdates];
self.tmpRow = indexPath.row;
self.selectedCell = (XPViewCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.tmpRow inSection:0]];
[self stretchAnimationIndexForRow:self.tmpRow tableViewCell:self.selectedCell];
[self.tableView endUpdates];
self.repeatClick = YES;
}
}
//伸長(zhǎng)
- (void)stretchAnimationIndexForRow:(NSInteger)indexForRow tableViewCell:(XPViewCell *)cell {
self.selectRow = indexForRow;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3];
cell.height=230;
[UIView commitAnimations];
}
//收縮
- (void)contractAnimationIndexForRow:(NSInteger)indexForRow tableViewCell:(XPViewCell *)cell {
self.selectRow = indexForRow;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3];
cell.height=70;
[UIView commitAnimations];
}