ios - 通过手势调整UITableViewCell的大小

标签 ios objective-c uitableview uigesturerecognizer

我试图在用户捏住单个UITableViewCell时调整其大小。当“捏”足够大时,我想执行segue。

因此在cellForRowAtIndexPath中。我分配/初始化一个UIPinchGestureRecognizer并将其附加到单元格。

 - (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;
}

在以下方法中,我想基于用户的手势来计算(并设置)选定行的高度,然后“重绘”它。就像我说的那样,如果手势完成并且单元格足够大,那么我想执行segue。
- (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;
  }

除了具有2个属性(sizeOfCell和selectedIndexPath)外,问题还在于更改uitableviewcell大小的外观,看起来不够“平滑”。当您捏住一个单元并执行搜索时,我想实现类似iOS 7 weather app之类的功能。

如果你们有一个更清洁的解决方案来解决我的问题,我将非常感激! :]

最佳答案

只是建议您尝试一下...不确定是否会给您带来平滑的结果....尝试使用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]; 
}

应该在@property sizeOfCell的setter中调用您的performSegue,并检查它何时大于5并执行performSegue。

祝好运。

关于ios - 通过手势调整UITableViewCell的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18348910/

相关文章:

ios - Xcode 7.3 中用于目标的预处理器宏

IOS/Swift - 实例成员 'app' 不能用于错误类型

ios - 返回时忽略 presentModalViewController

ios - iPhone 的 UIPopoverController 通过实现类别

ios - 如何更优雅地添加/删除表格 View 中的单元格?

swift - Swift 中带有 Struct String 的 TableView 部分

ios - 想要在 objective-c 中创建字节数组

ios - 在 Swift 中,如何从 dispatch_time_t 获取 NSDate?

ios - 基于数组对 NSFetchedResultsController 结果进行排序?

ios - Swift - 将数据从 TableView 传递到第三个 View Controller