简体   繁体   中英

How to change the uilabel text when animate an uiimageview

I had an UIimageview which animate images and I had a uilabel which describes about the image How to change the label content when we change the image

for (int i=0; i<[mutable_banner count]; i=i+3) 
        {
            NSLog(@"--------%d----------%@",i,[mutable_banner objectAtIndex:i]);
            addr = [mutable_banner objectAtIndex:i];
            addr = [addr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
            data_banner = [NSData dataWithContentsOfURL:[NSURL URLWithString:addr ] ];
            img_banner = [[UIImage alloc] initWithData:data_banner];

            [images addObject:img_banner];  

        }   


        array = [[NSArray alloc] initWithArray:images];

         NSLog(@"-array-----%d",[array count]);

        imgview_banner = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10,364 ,164)];//364.164
       // [imgview_banner setImage:[UIImage imageWithData:data_banner ]];  

        imgview_banner.animationImages=images;
        imgview_banner.animationDuration=30.0f;
        [imgview_banner startAnimating ];
        [view_banner addSubview:imgview_banner];

正如我在您的代码中看到的那样,您正在使用UIImageView动画,动画持续时间为30.f秒,因此要在更改任何图像时更改UILabel文本,您需要将NSTimer设置为timeInterval设置动画,在计时器的目标函数中,您可以相应地更改标签文本,为此,您可能需要保持currentIndex以根据图像显示文本,并做一些良好的工作,它将为您提供帮助。

You can store description of all images in an NSArray with same order as you add images in array . For example, array[0]'s description in imageDescription[0] and likewise.

Then when you start image animation, along with that call this method

index = 0;
[NSTimer scheduledTimerWithTimeInterval:30.0f target:self selector:@selector(showDesc) userInfo:nil repeats:YES];
[imgview_banner startAnimating ];

Then define showDesc() method as shown below :

- (void)showDesc {
    self.label.text = imageDescription[index];
    index ++;
    if (index == array.count) {
        index = 0;
    }
}

Declare a variable index as global.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM