2012-05-19 130 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.layer removeAllAnimations];其中MyView的是被動畫滾動視圖取消動畫。確保在頂部導入<QuartzCore/QuartzCore.h>以訪問CALayer方法。

編輯:您可能需要調用它,以確保它立即,而不是在下次運行runloop迭代之後:

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

或者,如果仍然無法正常工作,那麼可能只是更改選項UIView方法調用中的參數UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState應該可以工作,不需要調用removeAllAnimations。事實上,先嚐試一下。

+0

非常感謝!它適用於我,但是當我在10秒內使用它時。持續時間,動畫看起來是以正常速度開始時(當第一個字母顯示時),但隨後加速,並且當最後幾個字母可見時,它又具有正常速度。使用第二個代碼樣本,持續時間爲10秒。我會更新我的問題 – Aft3rmath

+0

我更新了我的答案以解決該問題 –

+0

你太棒了!還有一個問題,當我點擊按鈕時,我需要從頭開始重新開始動畫,如何停止當前動畫,然後再次調用[自動動畫]?因爲沒有當前動畫停止[自動動畫]將無法正常工作 – Aft3rmath