2014-02-10 23 views
0

我試圖在按下按鈕時在UIImageView容器中顯示34個連續圖像。Xcode:UIImageView圖像不更新

- (IBAction)buttonPressed { 
    [self updateUI:(0)]; 
} 

-(void)updateUI:(int)yesser{ 

    //if GO is pressed 

     //display 34 images MG_8842.PNG -> MG_8875.PNG stored in Supporting Files 
     for(int j = 0;j<34;j++){ 
      NSString *newPhoto =[NSString stringWithFormat:@"MG_88%i.PNG",42+j]; 
      NSLog(@"%@",newPhoto); 
      sleep(1); 
      [self setNextImage:newPhoto]; 
     } 
} 

-(void)setNextImage:(NSString*)nextImage{ 

    [ball performSelectorOnMainThread:@selector(setImage:) withObject: [UIImage imageNamed:nextImage] waitUntilDone:YES]; 

} 

但是,圖像沒有更新。當所有代碼完成並返回時,顯示第一個圖像。

我已經試過如下:
1)產品中心>清潔
2)重新安裝應用程序。
3)檢查所有區分大小寫。
4)測試了多個設置UIImageView圖像的方法。
5)檢查是否有34個靜態NSString值工作,而不是每次更改一個NSString
6)刪除並重新添加圖像到支持文件文件夾。
7)重命名所有圖像和更新代碼以匹配。
8)關閉AutoLayout。

任何幫助將非常感激。

感謝, 羅布

+0

我只能想象,使用'睡眠( )'會給你帶來更多的問題而不是幫助。你能不能只用一個'NSTimer'並安排它每秒調用'setNextImage:'? – SpacePyro

+0

顯示你的setImage方法 –

+0

我有一個偷偷的懷疑,你試圖使用睡眠複製動畫.. – amar

回答

0

我想你一般會希望避免使用sleep()如果你想更新UI。當你調用睡眠時,你告訴主線程暫停一段時間。由於用戶界面全部在主線程上完成,因此我可以想象,因爲它正在嘗試更新它的視圖,所以調用sleep()會阻止它完全更新。因此,爲什麼直到最後纔會看到更新。

正如我在我的評論中提到的,既然你想更新我想象的是UIImageView,你應該考慮使用NSTimer。這個類對於想要在特定時間間隔中安排定期更新很方便。在這種情況下,我們希望UIImageView開始更新分鐘buttonPressed被調用,所以我們可以做這樣的事情:

- (IBAction)buttonPressed:(id)sender { 
    [self updateUI]; 
} 

- (void)updateUI{ 
    // If you wanted to keep track of this NSTimer, you could hold onto it 
    // via a property. 
    // `fire` basically tells the timer to begin executing what's been set 
    // for the selector. 
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(setNextImage:) userInfo:nil repeat:YES]; 
    [timer fire]; 
} 

- (void)setNextImage { 
     // Here is where your logic would change. You would need some kind of way 
     // of figuring out which image to display. You could have an NSInteger 
     // property store how many times the method gets called and then 
     // determine which image to show from there, for example. 

     // Assume that with your new logic, you have acquired the image you 
     // want to display. 
     UIImage *image = imageToDisplay; 

     // Also assuming here that (based on your code), `ball` is the 
     // name of your UIImageView. 
     ball.image = imageToDisplay; 
} 

破壞計時器,只需調用[timer invalidate]

+0

非常感謝你。我添加了選擇下一張圖片的邏輯,並從buttonPressed方法中刪除了參數:id(sender)。後者在執行過程中導致了一個無效的參數問題,並不完全確定爲什麼但刪除修復了錯誤。我的僞動畫代碼(@amar)正在工作! – User1234231

-1

試試這個

- (IBAction)buttonPressed { 
    [self updateUI:(0)]; 
} 

-(void)updateUI:(int)yesser{ 

//if GO is pressed 

    //display 34 images MG_8842.PNG -> MG_8875.PNG stored in Supporting Files 
    for(int j = 0;j<34;j++){ 
     NSString *newPhoto =[NSString stringWithFormat:@"MG_88%i",42+j]; 
     NSLog(@"%@",newPhoto); 
     sleep(1); 
     [self setNextImage:newPhoto]; 
    } 
} 

-(void)setNextImage:(NSString*)nextImage{ 

    [ball performSelectorOnMainThread:@selector(setImage:) withObject: [UIImage imageNamed:nextImage] waitUntilDone:YES]; 
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)]; 
    imgView.image = [UIImage imageNamed:[NSString stringWithFormat: @"%@.png", nextImage]]; 
    [self.view addSubview:imgView]; 
} 
+2

這每當調用'setNextImage:'時,都會在父視圖中添加一個全新的'UIImageView'。我不認爲這一定會解決這個問題。 – SpacePyro

+2

這將引入新問題而不是解決當前問題。 – 2014-02-10 06:53:00

0

的UIImageView具有動畫圖像的選項,試試這個

imageView.animationImages = [NSArray arrayWithObjects: 
             [UIImage imageNamed:@"progress-1"], 
             [UIImage imageNamed:@"progress-2"], 
             [UIImage imageNamed:@"progress-3"], 
             [UIImage imageNamed:@"progress-4"], nil]; 
imageView.animationDuration = 2.0f; 
imageView.animationRepeatCount = 1; 
[imageView startAnimating]; 

你必須重命名順序的圖像,並把這個按鈕按..