Ios 独立线程上的独立运行文本行
已更新Ios 独立线程上的独立运行文本行,ios,iphone,uiscrollview,line,Ios,Iphone,Uiscrollview,Line,已更新 我想让运行文本行,哪种方式更好 -(void)viewDidLoad { CGSize mySize = CGSizeZero; mySize = [kGrindFMRunningText sizeWithFont:[UIFont fontWithName:@"Verdana" size:16] constrainedToSize:CGSizeMake(4000, 30) lineBreakMode:UILineBreakModeWo
我想让运行文本行,哪种方式更好
-(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 RemoveAllianimations];[CATransaction-commit]代码>对我不起作用UIViewAnimationOptionCurveLinear | UIViewAnimationOptionBeginFromCurrentState
工作起来很奇怪:首先,它不像我看到的那样执行完成块,或者可能执行,但动画不会再次启动。如果我单击按钮,什么也没发生,但当我第二次单击它时,动画从另一个方向开始,并减慢速度直到冻结。您应该能够更简单地使用嵌套的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
}];
}
默认情况下,动画使用uiviewmanimationoptioncurveeaseinout
animation选项。您需要UIViewAnimationOptionCurveLinear
选项。我已经更新了上面的代码以包含这一点
根据对这个问题的回答:您应该能够通过调用[myView.layer removeAllAnimations]来取消动画代码>其中myView是正在设置动画的滚动视图。确保在顶部导入
,以访问CALayer方法
编辑:您可能需要这样调用它,以确保它立即运行,而不是在下一次运行循环迭代之后运行:
[CATransaction begin];
[myView.layer removeAllAnimations];
[CATransaction commit];
或者,如果这仍然不起作用,那么可能只需将UIView方法调用中的选项参数更改为UIViewAnimationOptionCurveLinear | UIViewAnimationOptionBeginFromCurrentState
,就可以工作,而无需调用removeAllAnimations。事实上,先试试看。非常感谢!它对我有用,但当我用它的时候只有10秒。持续时间,动画在开始时(当第一个字母显示时)似乎以正常速度运行,但随后它加速了,当最后两个字母可见时,它又以正常速度运行。使用第二个代码示例,持续时间为10秒。我会更新我的问题你很棒!还有一个问题,当我点击按钮时,我需要动画从开始重新开始,我如何停止当前动画,然后再次调用[自动画]?因为如果没有当前动画,停止[自动画]将无法向您查询答案<代码>[CATTransaction begin];[myView.layer RemoveAllianimations];[CATransaction-commit]代码>对我不起作用UIViewAnimationOptionCurveLinear | UIViewAnimationOptionBeginFromCurrentState
工作起来很奇怪:首先,它不像我看到的那样执行完成块,或者可能执行,但动画不会再次启动。若我点击按钮,什么也并没有发生,但当我第二次点击它时,动画从另一个方向开始,并减速直到冻结。我将更新questionHm,看起来像在ios 4.3和更低版本上,这个永恒的动画不允许其他界面部件工作=\看看这个:您可能需要将UIViewAnimationOptionAllowUserInteraction
添加到动画选项中