1

我只想让标签的 TextColor 每次从左到右进入屏幕时使用arc4random ..

以下代码在每次应用加载时适用于 1 种随机颜色,但在离开视图时无效!

NSTimer* myTimer;

int y = 15;
int x = 0;

-(void)textView
{
    myLabel = [[UILabel alloc] initWithFrame :CGRectMake(0, 0, 550, 30)];
    myLabel.backgroundColor = [UIColor clearColor];
    myLabel.textColor = [UIColor colorWithRed:arc4random()%100/100.0 green:arc4random()%100/100.0 blue:arc4random()%100/100.0 alpha:1];
    myLabel.font = [UIFont boldSystemFontOfSize:25];
    myLabel.text = @"My Sample Application";

    [self.view addSubview:myLabel];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [NSTimer scheduledTimerWithTimeInterval:0.005 target:self selector:@selector(animatedText) userInfo:nil repeats:YES];

    [self textView];
    [self animatedText];
}

-(void)animatedText
{
    if ( myLabel.center.x < 640 )
    {
        x += 1;
    }
    else
    {
        x = 0;
        y =(arc4random() % 500 ) ;
    }
    myLabel.center = CGPointMake(x, y);
}
4

1 回答 1

2

颜色已设置-(void)textView- 只有在您的视图首次加载时才会调用。

复制myLabel.textColor = ...到您animatedText重置 x 和 y 位置的方法中。

于 2012-09-22T10:14:53.483 回答