2012-07-15 105 views
1

我有一個圖像視圖有兩個動畫圖像,每隔1秒發生一次。我想,當我的圖像視圖顯示這兩個圖像檢查動畫圖像是否在給定時間在圖像視圖中

我已經嘗試過這樣做的一個運行一些方法:

if(self.imageViewThatPerformsAnimation.image == [UIImage imageNamed: @"someImage"]) 
    [self doSomeMethod]; 

但是當我嘗試這樣做,運行它,[self doSomeMethod];總是跑去,不只是當圖像視圖正在顯示該圖像。

我在考慮具有改變一個布爾值,每一秒鐘,然後說

if (booleanValue==YES) 
    [self doSomeMethod] 

這只是我覺得有可能是一個更好的辦法的計時器。

+0

您的意思是做賦值操作符,「=」,而不是測試的平等?另外,你如何做你的圖像動畫? – Rob 2012-07-15 18:52:40

+0

我只有2個圖像在圖像視圖中,我使用屬性animationDuration和animationRepeatCount並將圖像放入數組中並執行啓動動畫。和對不起,對於兩個等於我的意思是2等於想測試它們是否相等 – bmende 2012-07-15 18:55:58

+0

我傾向於自己接管動畫,自己通過'NSTimer'或'performSelector'觸發由'afterDelay'觸發的圖像,然後你知道轉換髮生的時間,因此當圖像出現時你可以做任何你想做的事情。 – Rob 2012-07-15 19:19:39

回答

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]; 
}