user2645586
user2645586

Reputation: 71

Changing UILabel every 30 seconds- Iphone

I'm working on an iphone app and have a UILabel that I want to change every 30 seconds but keep the same frame so that one disappears and then another one is displayed. With this code all of the labels are drawn at once,then it terminates. I'm also using ARC.

-(void)viewDidLoad
{
    [super viewDidLoad];
    label1= [[UILabel alloc] initWithFrame:CGRectMake(80, 200, 100, 50)];
    label1.text=@"test";
    label1.backgroundColor= [UIColor clearColor];

    [self.view addSubview:label1];

    label2= [[UILabel alloc] initWithFrame:CGRectMake(80, 200, 100, 50)];

    label2.text=@"change";

    label2.backgroundColor= [UIColor clearColor];
    [self.view addSubview:label2];
...

    warmup = [[NSMutableArray alloc] initWithObjects:label1,label2,label3, nil];

    timer=[NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(rotatewarmup )userInfo:nil repeats:YES];
}


-(void)rotatewarmup
{

        for (NSUInteger i = 0; i < [warmup count]; i++) {

        UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(80, 200, 100, 50)];

        label.textColor=[UIColor whiteColor];

        label.text =[warmup objectAtIndex:i];

        NSString*string=[[NSString alloc] initWithFormat:@"%i"];
        [label setText:string];

        [self.view addSubview: label];
}

Upvotes: 0

Views: 1017

Answers (2)

user2828120
user2828120

Reputation: 233

This code change uilabel text every 30 seconds, first:

timer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(rotatewarmup )userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode];

second:

-(void)rotatewarmup
{

        for (UILabel* label in warmup) {
        NSString*string=[[NSString alloc] initWithFormat:@"%@%@",label.text,label.text];
        [label setText:string];}
}

If you want simple animation show-hide uilabel every 30 seconds:

-(void)viewDidLoad
{
     ....
    [self hideLabel:label3]
}
- (void) hideLabel: (UILabel*) label
{
    [UIView animateWithDuration:10.0
                     animations:^{
                         label.alpha = 0;
                     }
                     completion:^(BOOL finished) {
                         NSInteger nextIndex = [warmup indexOfObject:label] - 1;
                         if(nextIndex > 0)
                         {
                               UILabel* nextLabel = [warmup objectAtIndex:nextIndex];
                               [self hideLabel:nextLabel];
                         }else{
                               UILabel* nextLabel = [warmup objectAtIndex:nextIndex + 1];
                               [self showLabel:nextLabel];
                         }
                     }];
}

- (void) showLabel: (UILabel*) label
{
    [UIView animateWithDuration:10.0
                     animations:^{
                         label.alpha = 1;
                     }
                     completion:^(BOOL finished) {
                         NSInteger nextIndex = [warmup indexOfObject:label] + 1;
                         if(nextIndex < [warmup count] -1)
                         {
                               UILabel* nextLabel = [warmup objectAtIndex:nextIndex];
                               [self showLabel:nextLabel];
                         }else{
                               UILabel* nextLabel = [warmup objectAtIndex:nextIndex - 1];
                               [self hideLabel:nextLabel];
                         }
                     }];
}

Upvotes: 1

Woodstock
Woodstock

Reputation: 22926

You have two options you can either create one label and then use a timer to call a method that changes it's text (animated using core animation if you wish) OR

You could (somewhat more inefficiently) create two labels and using a timer call a method that changes the alpha of one to 0 and the alpha of the other to 1.

Again you can use animation blocks to make this process not jarring.

Let me know if you need me to expand on any of these steps?

Upvotes: 3

Related Questions