TableView下拉伸長(zhǎng)收縮效果

今天有點(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];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 14,994評(píng)論 4 61
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,671評(píng)論 25 709
  • (根據(jù)葉傾城老師授課整理。) 第一篇章:如何成為一個(gè)作家? 一、什么樣的人會(huì)成為作家? 第一種類型:心里有話要說,...
    蘇沫逃離閱讀 560評(píng)論 0 7
  • 去洞庭湖取魚/前行者 舅母子的姨娘以捕魚為業(yè),不是在安鄉(xiāng)大河里,就在岳陽轄區(qū)東洞庭湖上捕魚,哪里的魚多,就往哪里...
    前行者1一常德一自由人閱讀 668評(píng)論 0 0

友情鏈接更多精彩內(nèi)容