2014-10-16 1 views
2

테이블 뷰에서 애니메이션을로드하면 항목이 옆으로 밀어 넣어 져야하는 UITableViewController를 만들려고합니다. 세포는 기본 세포보다 높이가 약 5 ~ 7 배 더 큽니다. Google+ 로딩 애니메이션과 같은 애니메이션을 Android에서 볼 수 있습니다. (카드 애니메이션).UITableView에 대한 사용자 지정 애니메이션로드

TableView 클래스를 서브 클래스로 만들고 디스플레이에 새로운 항목을 표시 할 때로드하는 메서드를 재정의 한 다음 스프링 애니메이션을 사용하여 x 및 y 값이 점차적으로 증가하는 동안 셀을 동시에 회전합니다. 변경되었습니다.

this answer에서이 코드를 시도했지만 제대로 작동하지 않았습니다. 그 대답에 대한 상세화와 설명은 또한 매우 도움이 될 것입니다. 코드 :

- (void)animate 
{ 
[[self.tableView visibleCells] enumerateObjectsUsingBlock:^(UITableViewCell *cell, NSUInteger idx, BOOL *stop) { 
    [cell setFrame:CGRectMake(320, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)]; 
    [UIView animateWithDuration:1 animations:^{ 
     [cell setFrame:CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)]; 
    }]; 
}]; 
} 

도움이 될 것입니다. 에릭

답변

3

당신이 묘사하고있는 것 (나는 믿는다)의 완전한 기능적 예입니다. 빈 단일 뷰 프로젝트를 열고 스토리 보드에서 뷰 컨트롤을 삭제하고 단순히 UITableViewController를 만들고 그 클래스를 아래에 만든 UITableViewController 하위 클래스의 클래스로 설정했습니다.

는 헤더 파일에 추가 어디에도 없습니다, 그래서 그것을 이미 표시되는 뷰의 인덱스에 포함됩니다

#import "weeeTables.h" 

@interface weeeTables() 


@property (nonatomic, strong) NSMutableSet *shownIndexes; 
@property (nonatomic, assign) CATransform3D initialTransform; 

@end 

shownIndexes 제외했습니다, 그래서 우리는 다시 스크롤 애니메이션을 반복하지 않는다. initialTransform 속성은이

있는 viewDidLoad에서
@implementation weeeTables 

- (void)viewDidLoad { 
[super viewDidLoad]; 

CGFloat rotationAngleDegrees = -30; 
CGFloat rotationAngleRadians = rotationAngleDegrees * (M_PI/180); 
CGPoint offsetPositioning = CGPointMake(-20, -20.0); 

CATransform3D transform = CATransform3DIdentity; 
transform = CATransform3DRotate(transform, rotationAngleRadians, -50.0, 0.0, 1.0); 
transform = CATransform3DTranslate(transform, offsetPositioning.x, offsetPositioning.y, -50.0); 
_initialTransform = transform; 

_shownIndexes = [NSMutableSet set]; 
} 

, 우리는 설정 초기의 값이 변환 (이 화면에 오기 전에 당신이 그것을 원하는 위치) 셀의 초기 상태에 대한, 내가 값을 비트와 함께 연주 변환입니다 찾고있는 효과를 얻으려면 동일한 작업을 수행하는 것이 좋습니다. 그러나 셀은 현재 왼쪽 하단 모서리에서 애니메이션을 적용합니다. 위의 블록

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return 20; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return 385; 
} 




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"weeCell" forIndexPath:indexPath]; 


return cell; 
} 

모든 것이있는 UITableViewController 하위 클래스에 대한 매우 일반적인, 그리고 애니메이션을 추가하는 특별한 관련이없는, 그냥, 유일한 고유 한 값 여기에 스크롤하는 세포를 많이 추가 할 몇 가지 정적 번호를 설정 셀 식별자입니다.이 셀 식별자는 매우 신중하게 생각한 이름을 가지고 있습니다. 여기

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

if (![self.shownIndexes containsObject:indexPath]) { 

    [self.shownIndexes addObject:indexPath]; 
    UIView *weeeeCell = [cell contentView]; 

    weeeeCell.layer.transform = self.initialTransform; 
    weeeeCell.layer.opacity = 0.8; 

    [UIView animateWithDuration:.65 delay:0.0 usingSpringWithDamping:.85 initialSpringVelocity:.8 options:0 animations:^{ 
     weeeeCell.layer.transform = CATransform3DIdentity; 
     weeeeCell.layer.opacity = 1; 
    } completion:^(BOOL finished) {}]; 
} 
} 
@end 

먼저 우리가 디스플레이에 대해있어 셀이 shownIndexes 목록에 이미있는 경우 우리가 추가되지 않은 경우, 확인, 정말 모두 함께두고 사람의 다음 생성 이 현재 셀 내용보기에 대한 로컬 변수

그런 다음 해당 contentView에 대한 변환을 초기 변환 (ViewDidLoad에서 설정)으로 설정하십시오. 그러면 사용자가 볼 수 있기 전에 셀이 시작 위치로 이동 한 다음 해당 변환을 애니메이션에 다시 적용하여 ID 변환으로 되돌립니다. 나는 거기에도 불투명 함을 가지고있다.

희망이 있습니다.

+0

현재 학교에서 오늘 밤 시도 할 것입니다.) – Erik

+0

매력처럼 작동합니다.이 애니메이션을 변경할 수있는 방법이 있습니까? 대안이 있습니까? 우리는 CATransform3DIdentity를 설정했지만 다른 애니메이션을 생성하는 다른 것으로 변경할 수 있습니까? – Erik

+0

물론이 예제는 유연성 때문에 내가 선택한 경로입니다. 주요 아이디어는 두 단계 과정으로, 애니메이션을 시작하려는 위치에 셀 (또는이 경우에는 셀 내부의 내용보기)을 배치 한 다음 해당 셀을 애니메이션으로 위치에 배치합니다. 이 두 단계는 willDisplayCell 메서드에서 수행됩니다. @ james_womack의 응답은 셀을 움직일 수있는 완벽하게 유효한 방법이기도합니다. 그는 화면에서 전체 셀 프레임을 이동 한 다음 UIView 애니메이션 API를 사용하여 애니메이션 프레임을 움직입니다. – domitall

1

나는 당신이 초기 오프 스크린 프레임에있는 UITableViewCell을 설정 한 다음 비동기 적으로 해당 셀에 대한 애니메이션을 초기화 할 수 있습니다 tableView:willDisplayCell:forRowAtIndexPath:

사용하려고 할 것입니다.

나는 이것을 테스트 한 그리고 그것은 엑스 코드 마스터/상세 템플릿 당신은 다른 프리젠 테이션의 경우 특별한 케이스를 추가하는 단지가 필요합니다

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
     let frame = CGRectMake(-cell.frame.size.width, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height) 
     cell.frame = frame 

     UIView.animateWithDuration(1, delay: 0.1, options: UIViewAnimationOptions.CurveEaseInOut, animations: {() -> Void in 
      let endFrame = CGRectMake(100, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height) 
      cell.frame = endFrame 
     }) { (foo:Bool) -> Void in 
      // 
     } 
    } 

를 사용하여 작동합니다. 때때로 뷰에 다시 돌아 왔을 때 또는 셀이 뷰 밖으로 스크롤 된 후 다시 돌아올 때이 메서드가 호출됩니다.

관련 문제