gpt4 book ai didi

ios - UITableViewCell 在用户触摸或点击 Cell 时动画调整大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:38 25 4
gpt4 key购买 nike

我想在显示 detailedViewController 之前为 UITableViewCell 设置动画,如下图所示(单元格的大小调整得更小)。我从过去两天开始尝试过,并尝试了我为类似问题找到的所有其他答案。

我希望当用户将手指放在 UITableViewCell 上时,单元格会立即设置动画/调整大小。

enter image description here

UITableViewCell 是一个自定义单元格。我尝试在 UITableViewCell 子类的 setSelected:(BOOL)selected animated:(BOOL)animated 方法中为 cell.contentView 设置一个新框架。 UITableViewCell 没有像我预期的那样调整大小。

我还观察到,当用户从 UITableViewCell 上抬起手指时,会调用此方法。实际上我希望它在用户点击单元格时立即发生。如果未选择该单元格并且用户移动到其他某个单元格,则单元格的大小应调整回正常大小。

然后我做了一些关于从 UITableViewCell 接收触摸事件的搜索。我尝试添加 UILongPressGestureRecognizer 和 UITapGestureRecognizer 并在选择器中尝试像这样更改 cell.contentView 框架

cell.contentView.frame = CGRectMake(cell.frame.origin.x +20, cell.frame.origin.y, cell.frame.size.width-20, cell.frame.size.height);
[cell setNeedsDisplay];

但选定的单元格只是闪烁(单元格背景),它没有按我预期的那样调整大小。

任何建议都会很有帮助。提前致谢

最佳答案

这是在 Swift 5.0 中点击点击时制作单元格动画的更简单方法:

func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath)
{
let cell = tableView.cellForRow(at: indexPath)

UIView.animate(withDuration: 0.3) {

cell!.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
}
}

func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath)
{
let cell = tableView.cellForRow(at: indexPath)

UIView.animate(withDuration: 0.3) {

cell!.transform = .identity
}
}

关于ios - UITableViewCell 在用户触摸或点击 Cell 时动画调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26669545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com