ios 当UIImageView动画中的UIImage更改时更改文本

j13ufse2  于 2023-02-01  发布在  iOS
关注(0)|答案(1)|浏览(103)

在我的应用程序中,我需要设置一个动画,它将重复运行,直到我对它执行操作。我希望图像和文本沿着变化。我现在尝试建议的关键帧动画,但它只显示最后的变化,没有重复。我做了一些搜索,并查看了文档,但我肯定仍然错过了一些东西。
我的理解是animateKeyframesWithDuration以秒为单位,而相对开始和相对持续时间的值为0 - 1,因此我将每个值设置为0.2,因为有5个,0.2正好是五分之一。我做错了什么?所有NSLog都会触发,但除了最后一个事件外,没有任何事件发生实际更改。

[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];
nx7onnlm

nx7onnlm1#

好的,这就是我最后做的。我只有5个不同的“幻灯片”需要旋转,其中一些没有文本。现在,我把文本放入一个数组中来模拟我需要的内容。将来,这将是动态的,从服务器中提取。然后,我设置了一个NSInteger属性来链接所有内容。并从这个数组中提取,以沿着每个图像动画所需的时间。

-(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 = @"";
    }
    
}

相关问题