Ios 通过手势调整UITableViewCell的大小
我试图在用户挤压单个UITableViewCell时调整其大小。当“挤压”足够大时,我想表演一段 所以在cellForRowAtIndexPath中。我分配/初始化UIPinchGestureRecognitor并将其连接到单元格Ios 通过手势调整UITableViewCell的大小,ios,objective-c,uitableview,uigesturerecognizer,Ios,Objective C,Uitableview,Uigesturerecognizer,我试图在用户挤压单个UITableViewCell时调整其大小。当“挤压”足够大时,我想表演一段 所以在cellForRowAtIndexPath中。我分配/初始化UIPinchGestureRecognitor并将其连接到单元格 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
/** adding info to the cell **/
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(makeBiggerCell:)];
[cell addGestureRecognizer:pinchGesture];
return cell;
}
在下面的方法中,我希望根据用户的手势计算(并设置)所选行的高度,然后“重新绘制”。就像我说的,如果手势完成,并且单元格足够大,那么我想执行一个序列
- (void)makeBiggerCell:(UIPinchGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateChanged){
// I have a private property of NSIndexPath in order to keep
// track of the selected row.
self.selectedIndexPath = [self.tableView indexPathForCell:(UITableViewCell *)sender.view];
CGPoint windowPoint = [gesture locationOfTouch:1 inView:nil];
CGPoint tablePoint = [gesture locationInView:self.tableView];
self.sizeOfCell = fabsf(tablePoint.y - windowPoint.y);
// the MINIMUM_CELL_SIZE is the regular size of the cell
// this conditional tries to avoid small tableviewcells
// IMPORTANT: as long as the user keeps his fingers on the cell
// the 'resize' happens
if (self.sizeOfCell > MINIMUM_CELL_SIZE)
[self.tableView reloadRowsAtIndexPaths:@[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
}else if (sender.state == UIGestureRecognizerStateEnded){
if (sender.scale >=5) {
[self performSegueWithIdentifier:@"MySegue" sender:sender.view];
}
}
}
最后是为选定行设置高度的位置
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.selectedIndexPath && self.selectedIndexPath.row == indexPath.row && self.sizeOfCell)
return self.sizeOfCell + MINIMUM_CELL_SIZE;
else
return MINIMUM_CELL_SIZE;
}
除了拥有两个属性(sizeOfCell和selectedIndexPath)之外,问题在于更改uitableviewcell大小的外观看起来不够“平滑”。我想实现类似iOS 7天气应用程序的功能,当你捏一个手机并执行一个序列时
如果你们能为我的问题找到一个更干净的解决方案,我会非常感激的!:] 只是想推荐一些东西给你试试。。。不确定它是否会给你平滑的结果。。。。尝试使用pinchGesture的缩放设置大小,并在每次更改后将缩放设置回1
if (pinchGesture.state == UIGestureRecognizerStateChanged || pinchGesture.state == UIGestureRecognizerStateEnded) {
self.sizeOfCell.width *= pinchGesture.scale;
self.sizeOfCell.height *= pinchGesture.scale;
pinchGesture.scale = 1;
[self.tableView reloadRowsAtIndexPaths: @[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
}
您的performsgue应该在@property sizeOfCell的setter中调用,检查它何时大于5和performsgue
祝你好运 谢谢!你是对的。虽然效果并不像我预期的那样平滑,但效果很好。