2

当用户捏住它时,我正在尝试调整单个 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 天气应用程序的功能。

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

4

1 回答 1

0

Just to recommend something for you to try... not sure if it will give you smooth result.... try using pinchGesture's scale to set the size, and set the scale back to 1 after each change.

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

Your performSegue should be called in setter of the @property sizeOfCell, checking when the it's larger than 5 and performSegue.

Good luck.

于 2013-08-21T05:06:13.403 回答