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奇怪的工作:首先,它没有像我看到的那样做完成块,或者它可能会做,但是动画不会重新开始。如果我单击按钮没有任何反应,但是当我第二次单击它时,动画从另一个方向开始并减慢直到冻结。

4

1 回答 1

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选项。我已经更新了上面的代码以包含它。


根据对这个问题的回答:取消 UIView 动画?您应该能够通过调用[myView.layer removeAllAnimations];myView 是动画滚动视图来取消动画。确保在顶部导入<QuartzCore/QuartzCore.h>以访问 CALayer 方法。

编辑:您可能需要这样调用它以确保它立即运行,而不是在下一次 runloop 迭代之后运行:

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

或者,如果这仍然不起作用,那么可能只需更改 UIView 方法调用中的选项参数即可UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState,无需调用 removeAllAnimations。事实上,先尝试一下。

于 2012-05-19T20:32:47.610 回答