[cocoa-touch] 변경할 때 UITableView에 대한 reloadData에 애니메이션을 적용하십시오.

두 가지 모드가있는 UITableView가 있습니다. 모드 간을 전환하면 섹션마다 다른 수의 섹션과 셀이 있습니다. 이상적으로는 테이블이 커지거나 줄어들 때 멋진 애니메이션을 만듭니다.

여기에 내가 시도한 코드가 있지만 아무것도하지 않습니다.

CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.5];

[self.tableView reloadData];
[UIView commitAnimations];

내가 어떻게 할 수 있을지에 대한 생각?



답변

실제로 매우 간단합니다.

[_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

에서 문서 :

이 메소드를 호출하면 테이블보기가 데이터 소스에 지정된 섹션에 대한 새 셀을 요청합니다. 테이블 뷰는 기존 셀을 애니메이션 처리 할 때 새 셀 삽입을 애니메이션합니다.


답변

다음을 사용할 수 있습니다.

목표 -C

[UIView transitionWithView: self.tableView
                  duration: 0.35f
                   options: UIViewAnimationOptionTransitionCrossDissolve
                animations: ^(void)
 {
      [self.tableView reloadData];
 }
                completion: nil];

빠른

UIView.transitionWithView(tableView,
                          duration: 0.35,
                          options: .TransitionCrossDissolve,
                          animations:
{ () -> Void in
    self.tableView.reloadData()
},
                          completion: nil);

스위프트 3, 4 및 5

UIView.transition(with: tableView,
                  duration: 0.35,
                  options: .transitionCrossDissolve,
                  animations: { self.tableView.reloadData() }) // left out the unnecessary syntax in the completion block and the optional completion parameter

번거 로움이 없습니다. :디

UIViewAnimationOptionTransitions더 차가운 효과를 위해 원하는 것을 사용할 수도 있습니다 .

  • transitionNone
  • transitionFlipFromLeft
  • transitionFlipFromRight
  • transitionCurlUp
  • transitionCurlDown
  • transitionCrossDissolve
  • transitionFlipFromTop
  • transitionFlipFromBottom

답변

CATransition수업을 통해 더 많은 자유를 누리십시오.

페이딩에만 국한되지는 않지만 움직임도 가능합니다.


예를 들면 다음과 같습니다.

(가져 오기를 잊지 마십시오 QuartzCore)

CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.fillMode = kCAFillModeForwards;
transition.duration = 0.5;
transition.subtype = kCATransitionFromBottom;

[[self.tableView layer] addAnimation:transition forKey:@"UITableViewReloadDataAnimationKey"];

등을 type필요에 맞게 변경하십시오 kCATransitionFade.

스위프트에서의 구현 :

let transition = CATransition()
transition.type = kCATransitionPush
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.fillMode = kCAFillModeForwards
transition.duration = 0.5
transition.subtype = kCATransitionFromTop
self.tableView.layer.addAnimation(transition, forKey: "UITableViewReloadDataAnimationKey")
// Update your data source here
self.tableView.reloadData()

CATransition에 대한 참조

스위프트 5 :

let transition = CATransition()
transition.type = CATransitionType.push
transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
transition.fillMode = CAMediaTimingFillMode.forwards
transition.duration = 0.5
transition.subtype = CATransitionSubtype.fromTop
self.tableView.layer.add(transition, forKey: "UITableViewReloadDataAnimationKey")
// Update your data source here
self.tableView.reloadData()


답변

데이터 구조를 업데이트하면 다음과 같습니다.

[tableView beginUpdates];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:YES];
[tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:YES];
[tableView endUpdates];

또한 “withRowAnimation”은 부울이 아니라 애니메이션 스타일입니다.

UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle


답변

이 모든 답변은 하나의 섹션으로 UITableView를 사용한다고 가정합니다.

섹션이 두 개 이상인 상황을 정확하게 처리하려면 다음을 사용하십시오.

NSRange range = NSMakeRange(0, myTableView.numberOfSections);
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
[myTableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

(참고 : 섹션이 0 개 이상 있는지 확인해야합니다!)

이 코드로 데이터 소스를 동시에 업데이트하려고하면 NSInternalInconsistencyException이 발생할 수 있습니다. 이 경우 다음과 유사한 논리를 사용할 수 있습니다.

int sectionNumber = 0; //Note that your section may be different

int nextIndex = [currentItems count]; //starting index of newly added items

[myTableView beginUpdates];

for (NSObject *item in itemsToAdd) {
    //Add the item to the data source
    [currentItems addObject:item];

    //Add the item to the table view
    NSIndexPath *path = [NSIndexPath indexPathForRow:nextIndex++ inSection:sectionNumber];
    [myTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationAutomatic];
}

[myTableView endUpdates];


답변

이것에 접근하는 방법은 tableView에게 행과 섹션을 제거하고 추가하도록 지시하는 것입니다.

insertRowsAtIndexPaths:withRowAnimation:,
deleteRowsAtIndexPaths:withRowAnimation:,
insertSections:withRowAnimation:
deleteSections:withRowAnimation:

UITableView의 메소드.

이 메소드를 호출하면 테이블이 요청한 항목을 애니메이션 처리 한 다음 reloadData 자체를 호출하여이 애니메이션 이후의 상태를 업데이트 할 수 있습니다. 이 부분은 중요합니다. 모든 것을 애니메이션 처리하지만 테이블의 dataSource에서 반환 된 데이터를 변경하지 않으면 애니메이션이 완료된 후 행이 다시 나타납니다.

따라서 응용 프로그램 흐름은 다음과 같습니다.

[self setTableIsInSecondState:YES];

[myTable deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:YES]];

테이블의 dataSource 메소드가 검사 [self tableIsInSecondState](또는 무엇이든) 하여 올바른 새로운 섹션 및 행 세트를 반환하는 한 원하는 결과를 얻을 수 있습니다.


답변

최고 답변에 대해서는 언급 할 수 없지만 신속한 구현은 다음과 같습니다.

self.tableView.reloadSections([0], with: UITableViewRowAnimation.fade)

reloadSections의 첫 번째 인수에 업데이트하려는 섹션을 포함 할 수 있습니다.

문서에서 사용할 수있는 다른 애니메이션 :
https://developer.apple.com/reference/uikit/uitableviewrowanimation

페이드
삽입되거나 삭제 된 행은 테이블 뷰에서 페이드 인 또는 페이드 아웃됩니다.

오른쪽
삽입 된 행을 오른쪽에서 밀어 넣습니다. 삭제 된 행이 오른쪽으로 미끄러 져 나옵니다.

왼쪽
삽입 된 행을 왼쪽에서 밀어 넣습니다. 삭제 된 행은 왼쪽으로 미끄러 져 나옵니다.

top
삽입 된 행을 맨 위에서 밀어 넣습니다. 삭제 된 행은 위쪽으로 미끄러 져 나옵니다.

bottom
삽입 된 행을 아래쪽에서 밀어 넣습니다. 삭제 된 행은 아래쪽으로 미끄러 져 나옵니다.

case none
삽입되거나 삭제 된 행이 기본 애니메이션을 사용합니다.

middle
테이블 뷰는 이전 및 새 셀을 그들이 차지했거나 차지할 공간의 중앙에 유지하려고합니다. iPhone 3.2에서 사용할 수 있습니다.

자동
테이블보기는 적절한 애니메이션 스타일을 선택합니다. (iOS 5.0에서 도입되었습니다.)