2012-06-07 30 views
1

我有一個UImageView,我在界面生成器中用我的資源中的png(一雙眼睛)設置了它。然後我想用眨眼的動畫替換這個圖像(經過一段特定的時間)。用動畫替換靜態UIImageView

這是我使用的代碼被稱爲在viewWillAppear

NSString *fileName; 
    NSMutableArray *imageArray = [[NSMutableArray alloc] init]; 
    for(int i = 1; i < 12; i++) { 
     fileName = [NSString stringWithFormat:@"HDBlinkPage1/hd_eyes_blinking%d.png", i]; 
     [imageArray addObject:[UIImage imageNamed:fileName]]; 
    } 
    imgHDBlink.userInteractionEnabled = YES; 
    imgHDBlink.animationImages = imageArray; 
    imgHDBlink.animationDuration = 0.9; 
    imgHDBlink.animationRepeatCount = 1; 
    imgHDBlink.contentMode = UIViewContentModeScaleToFill; 
    //[self.view addSubview:imgHDBlink]; 
    [imgHDBlink startAnimating]; 

在viewWillAppear中我使用NSTimer觸發動畫每5秒:

[NSTimer scheduledTimerWithTimeInterval:5.0 
            target:self 
            selector:@selector(blinkAnimation) 
            userInfo:nil 
            repeats:YES]; 

的問題是,當我運行的應用程序,我根本沒有看到最初的靜態圖像。我只看到動畫每5秒鐘,但在這些動畫之間沒有睜開眼睛的圖像。任何人都可以請幫我解決這個問題,或指出我在正確的方向嗎?謝謝。

回答

1

5.0秒後添加動畫圖像。從UIImageView文檔:

該數組必須包含UIImage對象。您可以在數組中多次使用相同的圖像對象。將此屬性設置爲nil以外的值可隱藏圖像屬性表示的圖像。該屬性的值默認爲零。

如果事先設置了animationImages數組,它將不會顯示圖像。

編輯: (全部採用ARC)

- (void) viewDidLoad { 
    [super viewDidLoad]; 

    //Initialize self.imgHDBlink 
} 

- (void) viewDidAppear: (BOOL) animated { 
    [super viewDidAppear: animated]; 

    self.imgHDBlink.image = [UIImage imageNamed: @"static_image"]; 

    [NSTimer scheduledTimerWithTimeInterval: 5.0 
            target: self 
            selector: @selector(blinkAnimation:) 
            userInfo: nil 
            repeats: YES]; 
} 

- (void) blinkAnimation: (NSTimer*) timer { 

    self.imgHDBlink.animationImages = [NSArray array]; //Actually add your images here 
    [self.imgHDBlink startAnimating]; 

    [self.imgHDBlink performSelector: @selector(setAnimationImages:) withObject: nil afterDelay: self.imgHDBlink.animationDuration]; 
} 



//Remember this to stop crashes if we are dealloced 
- (void) dealloc { 
    [NSObject cancelPreviousPerformRequestsWithTarget: self 
              selector: @selector(blinkAnimation:) 
               object: nil]; 
} 
+0

所以,你可以運行由我一次嗎?我的印象是,我在5秒後添加了動畫圖像? – garethdn