我想为UITableViewCell
制作动画,如下图所示(单元格的尺寸缩小了),然后显示了detailViewController。我从最近两天开始尝试过,并尝试了其他所有我因类似问题而找到的答案。
我希望该单元在用户将手指放在UITableViewCell上后立即对其进行动画处理/调整大小。
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS91SnZYVS5qcGcifQ==” alt =“在此处输入图像描述”>
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];
但是所选单元格仅在闪烁(单元格背景),没有按我预期的那样调整大小。
任何建议都会很有帮助。在此先感谢
好吧,在阅读了一些文档并浏览了WWDC视频后,我能够解决这个问题。
我学到了重要的事情。
从不使用setFrame:在具有AutoLayoutEnabled的视图上。
请记住,我将自定义UITableViewCell xib更改为使用自动调整大小蒙版。然后在我的UITableViewController中,我使用了这些tableView:didHighlightRowAtIndexPath: tableView:didUnHighlightRowAtIndexPath:委托
这是我的View Controller中的代码段。
-(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);
}
我正在努力实现与您相似的目标。我试图在添加到自定义UITableViewCell的UIImageView中显示UIImage的一部分,并设置了约束以使我的imageview整个单元格的大小(类似于背景,特别是当您有一个自定义单元格时好)。我遵循@Joy为this question提供的答案,并创建了以下内容:
我将此添加到了viewDidLoad
:
self.currentSelection = -1;
// I changed his line below, so that the image resizing (cell resizing really) is constant for
//all my images. I guess you can use this to resize your background back and forth to whatever size you want to.
self.newCellHeight = self.tableView.frame.size.width*3/2;
然后我添加了这些:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//I added this to toggle a tap-tapagain kind of thing, so if the same cell is tapped twice, first tap expands and second tap collapses.
if (self.currentSelection==indexPath.row) {
self.currentSelection = -1;
}else{
self.currentSelection = indexPath.row;
}
// save height for full text label
// UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// animate
[tableView beginUpdates];
[tableView endUpdates];
}
//The rest is pretty much the same as his code
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
// do things with your cell here
// sentinel
self.currentSelection = -1;
// animate
[tableView beginUpdates];
[tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
int rowHeight;
if ([indexPath row] == self.currentSelection) {
rowHeight = self.newCellHeight;
} else rowHeight = 100;
return rowHeight;
}
最后一件事,如果要显示背景居中,并从中心“放大”和缩小。我将imageview内容模式设置为居中,如下所示:
[cell.myImageView setContentMode:UIViewContentModeCenter];
我从@EmptyStack和@erkanyildiz对this question的回答中学到了很多。
无论如何,我希望无论如何都对您有所帮助,如果没有任何帮助,对不起! :)
这是在Swift 5.0中轻按da动画的一种简单方法:
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
}
}
尝试此
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath)path
// or it must be some other method
{
[self.tableView beginUpdates];
[[self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:rowAnimation];
[self.tableView endUpdates];
}
在表视图委托方法中,您必须知道选择了哪个单元格。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath)path
{
YourTableClass *cell=[tableView cellForRowAtIndexPath:path];
if(cell.isTaped)
return increasedHeight;
else
return defaultHeight;
}