2012-04-30 13 views
3

DB에서 채우는 셀이있는 동적 테이블 뷰가 있습니다. 셀을 선택하면 다른 옵션을 선택할 가능성이 있습니다. 셀을 선택했을 때 다른 뷰를 밀어 넣는 방법을 알고 있지만이 방법을 그래픽으로보기 싫어합니다. 예를 들어 같은 셀을 뒤집어 옵션을 표시 한 다음 다시 스 와이프하여 뒤집을 수있는 것이 더 좋습니다. 또는 전체 셀이 화면에서 벗어나 옵션을 표시하거나 다른보기가 셀에서 아래로 내려온 다음 다시 슬라이드 할 수 있습니다.선택한 테이블 셀에 슬라이딩 뷰 추가하기

다음 중 가장 쉬운 방법은 무엇입니까? 누구나 올바른 방향으로 나를 가리킬 수 있습니까? 물론 코드는 필요 없으며 배우기 위해 왔고 무엇을보아야하는지 알 필요가 있습니다. 지금까지는 UITableViewCell을 서브 클래 싱하는 것에 대해 읽었지만 솔직히 아직 이해하지 못했습니다. 모든 입력 사항을 매우 높이 평가할 것입니다.

답변

5

전경 및 배경보기와 UIPanGestureRecognizer가있는 UITableViewCell 하위 클래스를 사용합니다. 이 인식기는 스 와이프를 트리거하고 전경 뷰의 이동을 처리합니다. https://github.com/spilliams/sparrowlike

중요한 비트 : 구현 아주 쉽게 보인다

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CustomCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; 
    [panGestureRecognizer setDelegate:self]; 
    [cell addGestureRecognizer:panGestureRecognizer]; 

    return cell; 
} 

#pragma mark - Gesture recognizer delegate 
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer 
{ 
    CustomCell *cell = (CustomCell *)[panGestureRecognizer view]; 
    CGPoint translation = [panGestureRecognizer translationInView:[cell superview] ]; 
    return (fabs(translation.x)/fabs(translation.y) > 1) ? YES : NO; 
} 

#pragma mark - Gesture handlers 

-(void)handlePan:(UIPanGestureRecognizer *)panGestureRecognizer 
{ 
    float threshold = (PAN_OPEN_X+PAN_CLOSED_X)/2.0; 
    float vX = 0.0; 
    float compare; 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell *)[panGestureRecognizer view] ]; 
    UIView *view = ((CustomCell *)panGestureRecognizer.view).frontView; 

    switch ([panGestureRecognizer state]) { 
     case UIGestureRecognizerStateBegan: 
      if (self.openCellIndexPath.section != indexPath.section || self.openCellIndexPath.row != indexPath.row) { 
       [self snapView:((CustomCell *)[self.tableView cellForRowAtIndexPath:self.openCellIndexPath]).frontView toX:PAN_CLOSED_X animated:YES]; 
       [self setOpenCellIndexPath:nil]; 
       [self setOpenCellLastTX:0]; 
      } 
      break; 
     case UIGestureRecognizerStateEnded: 
      vX = (FAST_ANIMATION_DURATION/2.0)*[panGestureRecognizer velocityInView:self.view].x; 
      compare = view.transform.tx + vX; 
      if (compare > threshold) { 
       [self snapView:view toX:PAN_CLOSED_X animated:YES]; 
       [self setOpenCellIndexPath:nil]; 
       [self setOpenCellLastTX:0]; 
      } else { 
       [self snapView:view toX:PAN_OPEN_X animated:YES]; 
       [self setOpenCellIndexPath:[self.tableView indexPathForCell:(CustomCell *)panGestureRecognizer.view] ]; 
       [self setOpenCellLastTX:view.transform.tx]; 
      } 
      break; 
     case UIGestureRecognizerStateChanged: 
      compare = self.openCellLastTX+[panGestureRecognizer translationInView:self.view].x; 
      if (compare > PAN_CLOSED_X) 
       compare = PAN_CLOSED_X; 
      else if (compare < PAN_OPEN_X) 
       compare = PAN_OPEN_X; 
      [view setTransform:CGAffineTransformMakeTranslation(compare, 0)]; 
      break; 
     default: 
      break; 
    } 
} 
-(void)snapView:(UIView *)view toX:(float)x animated:(BOOL)animated 
{ 
    if (animated) { 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:FAST_ANIMATION_DURATION]; 
    } 

    [view setTransform:CGAffineTransformMakeTranslation(x, 0)]; 

    if (animated) { 
     [UIView commitAnimations]; 
    } 
} 
+0

는 여기 구현을 찾을 수 있습니다, 말했다. 나는 내일 아침에 그것을 시도 할 것이다. 고맙습니다. – Aleph72

+0

필자는 불필요한 비트 연산자와 포인터를 저글링으로 추가하여 더 복잡하게 만들 수 있다고 생각합니다.) – vikingosegundo

+0

Ok. 이 효과는 내가하려고했던 것에 완벽합니다. 대단히 vikingosegundo 주셔서 감사합니다. – Aleph72

관련 문제