触发一定次数后更改NSTimer时间间隔

编程入门 行业动态 更新时间:2024-10-14 10:41:36
本文介绍了触发一定次数后更改NSTimer时间间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在下面的代码中,每个图片之间的 NSTimer 间隔设置为1秒。我的目标是将前两张图片hello.png和bye.png之后的间隔更改为4秒。

In the following code, the NSTimer interval is set at 1 second between each picture. My goal is to change the interval after the first two pictures, hello.png and bye.png, to 4 seconds.

- (void)viewDidLoad { [super viewDidLoad]; NSArray *imageNames = @[@"hello.png",@"bye.png",@"helloagain.png",@"bye again"]; self.images = [[NSMutableArray alloc] init]; for (int i = 0; i < imageNames.count; i++) { [self.images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]]; } self.animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 90)]; self.animationImageView.image = self.images[0]; [self.view addSubview:self.animationImageView]; self.animationImageView.userInteractionEnabled = TRUE; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES]; } -(void)changeImage:(NSTimer *) timer { if (counter == self.images.count - 1 ) { counter = 0; }else { counter ++; } self.animationImageView.image = self.images[counter]; }

推荐答案

使计时器对象成为成员变量。最初将动画时间设置为1秒。在回调中,使计时器无效,并根据计数器的不同,在1或4秒内创建一个新计时器。

Make the timer object a member variable. Initially set animation time as 1 second. In the callback invalidate the timer and create a new one with 1 or 4 seconds depending on the counter.

@interface ViewController () @property (strong,nonatomic) NSMutableArray *images; @property (strong,nonatomic) UIImageView *animationImageView; { NSTimer *_timer; } @end @implementation ViewController { NSInteger counter; } - (void)viewDidLoad { [super viewDidLoad]; NSArray *imageNames = @[@"hello.png",@"bye.png",@"helloagain.png",@"bye again"]; self.images = [[NSMutableArray alloc] init]; for (int i = 0; i < imageNames.count; i++) { [self.images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]]; } self.animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 90)]; self.animationImageView.image = self.images[0]; [self.view addSubview:self.animationImageView]; self.animationImageView.userInteractionEnabled = TRUE; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)]; singleTap.numberOfTapsRequired = 1; singleTap.numberOfTouchesRequired = 1; [self.animationImageView addGestureRecognizer:singleTap]; _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES]; } -(void)changeImage:(NSTimer *) timer { [_timer invalidate]; if (counter == self.images.count - 1 ) { counter = 0; }else { counter ++; } if(counter == 0 || counter == 1) { _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES]; } else if(counter == 2 || counter == 3) { _timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES]; } self.animationImageView.image = self.images[counter]; }

更多推荐

触发一定次数后更改NSTimer时间间隔

本文发布于:2023-11-22 03:47:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615859.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:间隔   次数   时间   NSTimer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!