今天有点空余时间,看下别人写的一个表视图下拉效果实现伸长/收缩效果 实现效果图如下:
具体代码如下,实现的功能简单。
这两个方法,是配合起来使用的,标记了一个tableView的动画块。\n分别代表动画的开始开始和结束。两者成对出现,可以嵌套使用。一般,在添加,删除,选择 tableView中使用,并实现动画效果。\n在动画块内,不建议使用reloadData方法,如果使用,会影响动画。
[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;
/**重复点击标识*/
@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;
}
}
//伸长
- (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];
}