我只想让标签的 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);
}