You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为UITableView添加上下滑动动画以实现隐藏与显示效果?

给UITableView添加上下滑动展开/收起动画的实现方案

当然可以给你的UITableView添加上下滑动的展开/收起动画!你原来的代码是直接通过hidden属性切换显示状态,这种方式没有过渡效果。我们可以通过动态修改tableView的高度配合UIKit的动画API来实现你想要的下拉展开、向上收起的效果,下面分两种常见布局场景给出具体实现:

场景1:使用Frame布局

如果你的tableView是通过frame来设置位置和大小的,可以按以下步骤修改:

1. 保存原始Frame信息

首先在你的ViewController里添加属性来保存tableView的原始frame:

@property (nonatomic, CGRect) originalTableViewFrame;

然后在viewDidLoad中初始化这个属性,并处理初始隐藏状态:

- (void)viewDidLoad {
    [super viewDidLoad];
    // 保存tableView的原始位置和高度
    self.originalTableViewFrame = self.accountBalanceTableView.frame;
    
    // 如果初始是隐藏状态,先把高度设为0
    if (self.accountBalanceTableView.isHidden) {
        CGRect hiddenFrame = self.originalTableViewFrame;
        hiddenFrame.size.height = 0;
        self.accountBalanceTableView.frame = hiddenFrame;
    }
}

2. 修改按钮点击方法实现动画

替换你原来的mPesoAccount:方法,用UIView的动画块来实现平滑过渡:

- (IBAction)mPesoAccount:(id)sender {
    [UIView animateWithDuration:0.3 animations:^{
        CGRect targetFrame = self.originalTableViewFrame;
        if (self.accountBalanceTableView.frame.size.height > 0) {
            // 收起:将高度改为0
            targetFrame.size.height = 0;
        } else {
            // 展开:恢复原始高度
            targetFrame.size.height = self.originalTableViewFrame.size.height;
        }
        self.accountBalanceTableView.frame = targetFrame;
    } completion:^(BOOL finished) {
        // 动画结束后设置hidden,避免收起后误触
        self.accountBalanceTableView.hidden = (self.accountBalanceTableView.frame.size.height == 0);
    }];
}

场景2:使用Auto Layout布局(推荐)

如果你的项目用Auto Layout来管理界面(现在大部分iOS项目都会用),通过修改高度约束的方式会更适配屏幕旋转、自适应布局等场景,步骤如下:

1. 添加高度约束的IBOutlet

在Storyboard中给tableView添加高度约束,然后拖一个IBOutlet到ViewController:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeightConstraint;
@property (nonatomic, CGFloat) originalTableViewHeight;

2. 初始化原始高度

在viewDidLoad中保存原始高度并处理初始隐藏状态:

- (void)viewDidLoad {
    [super viewDidLoad];
    // 保存tableView的原始高度约束值
    self.originalTableViewHeight = self.tableViewHeightConstraint.constant;
    
    // 如果初始是隐藏状态,先把高度约束设为0
    if (self.accountBalanceTableView.isHidden) {
        self.tableViewHeightConstraint.constant = 0;
        [self.view layoutIfNeeded];
    }
}

3. 修改按钮点击方法实现动画

同样用动画块来修改约束并触发布局更新:

- (IBAction)mPesoAccount:(id)sender {
    [UIView animateWithDuration:0.3 animations:^{
        if (self.tableViewHeightConstraint.constant > 0) {
            // 收起:将高度约束设为0
            self.tableViewHeightConstraint.constant = 0;
        } else {
            // 展开:恢复原始高度约束值
            self.tableViewHeightConstraint.constant = self.originalTableViewHeight;
        }
        // 触发界面布局更新,让动画生效
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        // 动画结束后设置hidden
        self.accountBalanceTableView.hidden = (self.tableViewHeightConstraint.constant == 0);
    }];
}

额外说明

  • 你可以调整animateWithDuration:的参数(比如改成0.4)来控制动画的快慢,找到最适合的节奏。
  • 在completion回调中设置hidden是为了让tableView完全收起后不再响应触摸事件,提升用户体验。

内容的提问来源于stack exchange,提问作者KikX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:55:24