我有一个图像视图,有两个动画图像,以1秒为间隔出现。我希望在我的图像视图显示这两个图像之一时运行一些方法
我已经尝试过:
if(self.imageViewThatPerformsAnimation.image == [UIImage imageNamed: @"someImage"])
[self doSomeMethod];
但是当我尝试并运行它时,[self doSomeMethod];
总是运行,而不仅仅是图像视图显示那个图像。
我正在考虑让计时器每隔一秒更改一个布尔值然后说
if (booleanValue==YES)
[self doSomeMethod]
只是我觉得可能有更好的方法。
答案 0 :(得分:1)
如果您想使用NSTimer
,可能会显示如下:
@interface MyViewController ()
{
NSTimer *_timer;
NSArray *_images;
NSInteger _currentImageIndex;
}
@end
@implementation MyViewController
@synthesize imageview = _imageview;
- (void)viewDidLoad
{
[super viewDidLoad];
_images = [NSArray arrayWithObjects:
[UIImage imageNamed:@"imgres-1.jpg"],
[UIImage imageNamed:@"imgres-2.jpg"],
[UIImage imageNamed:@"imgres-3.jpg"],
[UIImage imageNamed:@"imgres-4.jpg"],
nil];
_currentImageIndex = -1;
[self changeImage];
// Do any additional setup after loading the view.
}
- (void)changeImage
{
_currentImageIndex++;
if (_currentImageIndex >= [_images count])
_currentImageIndex = 0;
self.imageview.image = [_images objectAtIndex:_currentImageIndex];
if (_currentImageIndex == 0)
[self doSomething];
}
- (void)startTimer
{
if (_timer) {
[_timer invalidate];
_timer = nil;
}
_timer = [NSTimer timerWithTimeInterval:1.0
target:self
selector:@selector(changeImage)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
}
- (void)stopTimer
{
[_timer invalidate];
_timer = nil;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self startTimer];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[self stopTimer];
}