2010-08-09 7 views
0

나는 RSS 피드 배열로 끝날 것이고, 라벨의 아래쪽에 그것들을 표시하기를 원할 것이다. 배열의 각 피드를 애니메이션으로 처리하고 싶습니다.UILabel 페이드 인/아웃 애니메이션하기

이것은 내가 지금까지 애니메이트 한 것입니다. 페이드를 위해 작동하지만 배열의 마지막 항목에만 애니메이션을 적용합니다.

feed = [[UILabel alloc] initWithFrame:CGRectMake(0,380,320,43)]; 
[self.view addSubview:feed]; 

feed.alpha=1; 

NSArray *feeds = [NSArray arrayWithObjects:[NSString stringWithFormat:@"1234567"],[NSString stringWithFormat:@"qwerty"],[NSString stringWithFormat:@"asdfgh"],nil]; 

for (NSString* f in feeds){ 

    feed.text=f; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    [UIView setAnimationDuration:2.0f]; 
    feed.alpha=0; 
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
    [UIView commitAnimations]; 

} 

간단합니다.

감사합니다.

답변

7

먼저 더 나은 명명 규칙을 고려해야합니다. UILabel을 호출하면 피드가 돌아와서 코드를 살펴볼 때 나중에 도움이되지 않습니다. 나는 그것의 이름을 feedLabel이라고 할 것이다. 그러면 피드 목록을 반복 할 때 for (NSString *feed in feeds) 일 수 있으며 더 이해할 수 있습니다. 그리고 그렇게 될 것입니다 feedLabel.text = feed;.

어쨌든 코드에서 볼 수있는 문제는 반복적으로 알파를 루프에 0으로 설정하고 있지만 다시 설정하지 않는다는 것입니다. 즉, 알파 값을 변경하지 않는 것입니다. 모든 반복에서 동일하게 유지됩니다.

어쩌면 당신은 당신이하려는 일을 분명히 할 수 있습니다. 텍스트의 변경 사항 사이에서 텍스트를 희미하게하려면 다른 애니메이션과 방법이 필요합니다. 루프 대신에 didStopSelector가 텍스트를 설정하고 다음 텍스트를 시작하도록 애니메이션을 연결하십시오. 같은 : 내가 코드를 시도

- (void)performAnimation; 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    [UIView setAnimationDuration:2.0f]; 
    feed.alpha=0; 
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)]; 
    [UIView commitAnimations]; 
} 

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag 
{ 
    feed.alpha = 1.0; 
    NSString *nextFeed = [self getNextFeed]; // Need to implement getNextFeed 
    if (nextFeed) 
    { 
    // Only continue if there is a next feed. 
    [feed setText:nextFeed]; 
    [self performAnimation]; 
    } 
} 
0

, 그것은 제 1 공급에 페이드 아웃,하지만 animationDidStop 이벤트를 입력하지 않습니다. 그게 왜 다시 performAnimation을 호출 할 수 없습니다. 애니메이션 (델리게이트 또는 프로토콜 등)에 대한 설정이 있습니까?

+1

[UIView setAnimationDelegate : self]를 호출해야합니다. – joec

+0

joec, 게시물이 정답입니다. 댓글 대신 답변으로 게시해야 크레딧을 얻을 수 있습니다. –

관련 문제