iosuiimageviewuiimageuitextviewuianimation

Change Text When UIImage in a UIImageView Animation Changes


In my app, I need to set up an animation that will run on repeat until I perform an action on it. I want both an image to change, and text change along with it. I am trying now the suggested key frame animations, but it is only showing the last change and no repeats. I have done some searches and looking at the documentation, but I'm definitely still missing something.

It was my understanding that the animateKeyframesWithDuration is measured in seconds, while relative start and relative duration are 0 - 1 in value, so I set each to be 0.2, as there are 5, and 0.2 is exactly a fifth. What am I doing wrong? All of the NSLogs fire, but none of the events actually change, other than the final one.

 [UIView animateKeyframesWithDuration:70 delay:0 options:UIViewKeyframeAnimationOptionRepeat animations:^{
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.2 animations:^{
            NSLog(@"a1");
            self.eventsText.text = nil;
            theImageView.image = [UIImage imageNamed:@"welcome.png"];
        }];
        [UIView addKeyframeWithRelativeStartTime:0.2 relativeDuration:0.2 animations:^{
            NSLog(@"a12");
            theImageView.image = [UIImage imageNamed:@"pleasepray.png"];

            self.eventsText.text = @"Test2";
        }];
        [UIView addKeyframeWithRelativeStartTime:0.4 relativeDuration:0.2 animations:^{
            NSLog(@"a13");
            theImageView.image = [UIImage imageNamed:@"comingevents.png"];

            self.eventsText.text = @"Test3";
        }];
        [UIView addKeyframeWithRelativeStartTime:0.6 relativeDuration:0.2 animations:^{
            NSLog(@"a14");
            theImageView.image = [UIImage imageNamed:@"birthdays.png"];

            self.eventsText.text = nil;
        }];
        [UIView addKeyframeWithRelativeStartTime:0.8 relativeDuration:0.2 animations:^{
            NSLog(@"a14");
            theImageView.image = [UIImage imageNamed:@"ournumbers.png"];

            self.eventsText.text = @"Test4";
        }];
    } completion:nil];

Solution

  • Ok, so here's what I ended up doing. I only have 5 different "slides" that I need to rotate through, and some of them without text. For now, I put text into an Array to simulate what I need. In the future, this will be dynamic, pulled from a server. I then set up an NSInteger property to link everything together, and pull from this array to go along with how long each animation of the images takes.

        -(void)announcementsTime {
       
        [self rotatewarmup];
        _timer = [NSTimer scheduledTimerWithTimeInterval:14.0 target:self selector:@selector(rotatewarmup )userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer: _timer forMode: NSDefaultRunLoopMode];
    }
    -(void)rotatewarmup
    {
        theImageView.hidden = NO;
        NSArray *imagesArray = [NSArray arrayWithObjects:
                                [UIImage imageNamed:@"welcome.png"],
                                [UIImage imageNamed:@"pleasepray.png"],
                                [UIImage imageNamed:@"comingevents.png"],
                                [UIImage imageNamed:@"birthdays.png"],
                                [UIImage imageNamed:@"contribution2.png"],
                                nil];
        if (self.songNumber == 0) {
            
            if (_firingTime == 4) {
                
                self.eventsText.text = self.allInfo[_firingTime];
                theImageView.image = imagesArray[_firingTime];
               
               
                _firingTime = 0;
            }
            else {
               
                self.eventsText.text = self.allInfo[_firingTime];
                theImageView.image = imagesArray[_firingTime];
                NSInteger newFiring = _firingTime + 1;
                _firingTime = newFiring;
            }
            
        }
        
        else {
            self.eventsText.text = @"";
        }
        
    }