Pri*_*m51 5 iphone objective-c uitableview ios
我想在显示 detailViewController 之前为UITableViewCell下图中的类似动画(单元格变得更小)。我从过去两天开始尝试过,并尝试了我为类似问题找到的所有其他答案。
我希望单元在用户将手指放在 UITableViewCell 上时立即动画/调整大小。

UITableViewCell 是一个自定义单元格。我尝试在 UITableViewCell 子类的setSelected:(BOOL)selected animated:(BOOL)animated方法中为 cell.contentView 设置一个新框架。UITableViewCell 没有像我预期的那样调整大小。
我还观察到,当用户从 UITableViewCell 抬起手指时会调用此方法。实际上我希望它在用户点击单元格时立即发生。如果未选择单元格并且用户移动到其他某个单元格,则单元格应调整回正常大小。
然后我做了一些关于从 UITableViewCell 接收触摸事件的搜索。我尝试添加一个UILongPressGestureRecognizer and 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];
Run Code Online (Sandbox Code Playgroud)
但是选定的单元格只是闪烁(单元格背景)它没有像我预期的那样调整大小。
任何建议都会非常有帮助。提前致谢
这是在 Swift 5.0 中点击 da cell 动画的更简单方法:
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
}
}
Run Code Online (Sandbox Code Playgroud)
好的,在阅读了一些文档并浏览了 WWDC 视频之后,我能够解决这个问题。
我学到了一件重要的事情。
切勿在启用 AutoLayout 的视图上使用 setFrame:。
记住这一点,我将我的 Custom UITableViewCell xib 更改为使用 AutoResizing 蒙版。然后在我的 UITableViewController 中我使用了这些tableView:didHighlightRowAtIndexPath: tableView:didUnHighlightRowAtIndexPath: delegates
这是我的视图控制器的代码片段。
-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"I'm Highlighted at %ld",(long)indexPath.section);
CustomTableViewCell *cell = (CustomTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
originalCellSize = cell.frame;
//Using animation so the frame change transform will be smooth.
[UIView animateWithDuration:0.1f animations:^{
cell.frame = CGRectMake(cell.frame.origin.x +20, cell.frame.origin.y, cell.frame.size.width-40, cell.frame.size.height);
}];
}
-(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = (CustomTableViewCell *)[self.tableableView cellForRowAtIndexPath:indexPath];
[UIView animateWithDuration:0.1f animations:^{
cell.frame = CGRectMake(0, originalCellSize.origin.y, self.view.bounds.size.width, 180) ;
}];
originalCellSize = CGRectMake(0, 0, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6912 次 |
| 最近记录: |