2012-05-19 4 views
0

업데이트 됨
실행중인 텍스트 줄을 만들고 싶습니다.독립적 인 스레드에서 독립적으로 실행중인 텍스트 줄

-(void)viewDidLoad 
    { 
     CGSize mySize = CGSizeZero; 
      mySize = [kGrindFMRunningText sizeWithFont:[UIFont fontWithName:@"Verdana" size:16] constrainedToSize:CGSizeMake(4000, 30) lineBreakMode:UILineBreakModeWordWrap]; 

     runningText = [[UIScrollView alloc] initWithFrame:CGRectMake(60, -5, 260, 50)]; 
      grind_fm_text = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, mySize.width, 30)]; 
      [grind_fm_text setUserInteractionEnabled:NO]; 
      [grind_fm_text setBackgroundColor:[UIColor clearColor]]; 
      [grind_fm_text setTextColor:[UIColor whiteColor]]; 
      [grind_fm_text setFont:[UIFont fontWithName:@"Verdana" size:16]]; 
      [grind_fm_text setText:kGrindFMRunningText]; 
      [runningText addSubview:grind_fm_text]; 
      [grind_fm_text release]; 
      [self animate]; 
    } 


     - (void)animate 
{ 
    [UIView animateWithDuration:10.0 delay:0. options:UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState animations:^{ 
     grind_fm_text.frame = CGRectMake(-grind_fm_text.frame.size.width, 15, grind_fm_text.frame.size.width, grind_fm_text.frame.size.height); 
     // Do your animation in one direction until text is gone 
    } completion:^(BOOL finished){ 
     grind_fm_text.frame = CGRectMake(260, 15, grind_fm_text.frame.size.width, grind_fm_text.frame.size.height); 
     // Move scroll position back to original position 
     [self animate]; // Then call animate again to repeat 
    }]; 
} 

-(void)songChange 
{ 
    CGSize mySize = CGSizeZero; 
    mySize = [result sizeWithFont:[UIFont fontWithName:@"Verdana" size:16] constrainedToSize:CGSizeMake(4000, 30) lineBreakMode:UILineBreakModeWordWrap]; 
    grind_fm_text.frame = CGRectMake(grind_fm_text.frame.origin.x, 15, mySize.width, 30); 
    grind_fm_text.text = result;; 
    [self animate]; 
} 

- (void)startStopStream { 
     [streamer stop]; 
     //[bufferIndicator stopAnimating]; 
     [CATransaction begin]; 
     [self.view.layer removeAllAnimations]; 
     [CATransaction commit]; 
     grind_fm_text.text = kGrindFMRunningText; 
     CGSize mySize = CGSizeZero; 
     mySize = [kGrindFMRunningText sizeWithFont:[UIFont fontWithName:@"Verdana" size:16] constrainedToSize:CGSizeMake(4000, 30) lineBreakMode:UILineBreakModeWordWrap]; 
     grind_fm_text.frame = CGRectMake(grind_fm_text.frame.origin.x, 15, mySize.width, 30); 
     [self animate]; 
} 

[CATransaction begin]; [myView.layer removeAllAnimations]; [CATransaction commit];은 저에게 적합하지 않습니다. UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState은 이상하게 작동합니다. 먼저 보았 듯이 완료 블록을 수행하지 않거나 그렇지는 않지만 애니메이션이 다시 시작되지 않습니다. 그리고 버튼을 클릭해도 아무 일도 일어나지 않지만 두 번 클릭하면 애니메이션이 다른 방향에서 시작되고 멈출 때까지 느려집니다.

답변

1

중첩 된 UIView 애니메이션 블록을 사용하면보다 간단하게이 작업을 수행 할 수 있습니다. 애니메이션 블록에서 한 방향으로 스크롤하고 완성 블록에서 다른 방향으로 스크롤 한 다음 해당 애니메이션의 완료 블록에서 애니메이션 기능을 다시 호출하여 반복합니다. 이 같은

뭔가 :

- (void)animate 
{ 
    [UIView animateWithDuration:10.0 delay:0. options:UIViewAnimationOptionCurveLinear animations:^{ 
     // Do your animation in one direction 
    } completion:^(BOOL finished){ 
     [UIView animateWithDuration:10.0 delay:0. options:UIViewAnimationOptionCurveLinear animations:^{ 
      // Do your animation in the other direction 
     } completion:^(BOOL finished){ 
      [self animate]; 
     }]; 
    }]; 
} 

또는 당신이, 같은 다시 그것을 통해 모든 방법을 스크롤하려면 :

- (void)animate 
{ 
    [UIView animateWithDuration:10.0 delay:0. options:UIViewAnimationOptionCurveLinear animations:^{ 
     // Do your animation in one direction until text is gone 
    } completion:^(BOOL finished){ 
     // Move scroll position back to original position 
     [self animate]; // Then call animate again to repeat 
    }]; 
} 

그것은 기본적으로 같은 소리를, 애니메이션은 UIViewAnimationOptionCurveEaseInOut 애니메이션 옵션을 사용합니다. UIViewAnimationOptionCurveLinear 옵션이 필요합니다. 내가 위의 코드를 포함하도록 업데이트했습니다.


이 질문에 대한 답변에 따라 경찰 : Cancel a UIView animation? 당신은 MYVIEW 애니메이션되는 스크롤 뷰가 어디 [myView.layer removeAllAnimations];를 호출하여 애니메이션을 취소 할 수 있어야한다. CALayer 메소드에 액세스하려면 상단에 <QuartzCore/QuartzCore.h>을 가져 오십시오.

편집 :

[CATransaction begin]; 
[myView.layer removeAllAnimations]; 
[CATransaction commit]; 

또는 그 여전히 작동하지 않는 경우, 다음 가능성이 바로 옵션을 변경 : 당신은 다음 runloop 반복 직후가 아닌 실행 확인하기 위해 다음과 같이 호출해야 할 수도 있습니다 UIView 메서드의 매개 변수는 UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState으로 호출해야 removeAllAnimations를 호출 할 필요가 없습니다. 사실, 먼저 시도해보십시오.

+0

고맙습니다. 그것은 나를 위해 작동하지만, 10 초와 함께 사용할 때. 애니메이션은 처음에는 정상 속도 (첫 번째 문자가 표시 될 때) 인 것처럼 보였지만 속도가 빨라졌으며 마지막 두 글자가 표시되면 정상 속도로 다시 나타납니다. 10 초 동안 두 번째 코드 샘플을 사용하십시오. 내 질문을 업데이 트하겠습니다 – Aft3rmath

+0

그 주소를 내 대답을 업데이 트 –

+0

당신은 위대한 분입니다! 그리고 한 가지 더 질문합니다. 버튼을 클릭하면 애니메이션이 시작될 때부터 시작해야합니다. 현재 애니메이션을 중단하고 [self animation]을 다시 호출하려면 어떻게해야합니까? 현재 애니메이션을 중지하지 않으면 [자동 애니메이션]이 작동하지 않습니다. – Aft3rmath