1

arc4randomを使用して、ラベルのTextColorを左から右に画面に入るたびにランダムな色にしたいだけです。

次のコードは、アプリが読み込まれるたびに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ます-これは、ビューが最初に読み込まれたときにのみ呼び出されます。

xとyの位置をリセットするメソッドにコピーmyLabel.textColor = ...します。animatedText

于 2012-09-22T10:14:53.483 に答える