emotality
emotality

Reputation: 13035

Simple label "screensaver" help needed

I would just like to make the Label's TextColor a random Color each time it enters the screen from left to right, using arc4random..

The following code works for 1 random color each time the app loads, but not when it leaves the view!

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);
}

Upvotes: 1

Views: 99

Answers (1)

ttarik
ttarik

Reputation: 3853

The colour is set in -(void)textView - which is only ever called when your view first loads.

Copy myLabel.textColor = ... into your animatedText method where you reset the x and y position.

Upvotes: 2

Related Questions