已更新
我想制作正在运行的文本行,哪种方式更好呢?
-(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
奇怪地工作:首先,它不会像我看到的那样完成块,或者它可能会完成,但动画不会重新开始。如果我点击按钮没有发生任何事情,但当我第二次点击它时,动画从另一个方向开始并减慢直到冻结。
答案 0 :(得分: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。事实上,先尝试一下。