簡體   English   中英

通過手勢調整UITableViewCell的大小

[英]Resize UITableViewCell by gesture

我正在嘗試在用戶捏住單個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]; 
}

您的performSegue應該在@property sizeOfCell的setter中調用,檢查它何時大於5並執行performSegue。

祝好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM