簡體   English   中英

用動畫替換靜態UIImageView

[英]Replacing static UIImageView with animation

我有一個UImageView ,它是在我的資源中用png(雙眼)在界面生成器中設置的。 然后,我想用眨眼的動畫替換此圖像(在特定的時間后)。

這是我使用過的在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秒看到一次動畫,但是在這些動畫之間沒有睜開眼睛的圖像。 誰能幫我解決這個問題或為我指明正確的方向? 謝謝。

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

該數組必須包含UIImage對象。 您可以在數組中多次使用同一個圖像對象。 將此屬性設置為nil以外的其他值將隱藏由image屬性表示的圖像。 默認情況下,此屬性的值為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];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM