2010-11-04 46 views
0

我知道這裏最有可能是嚴重錯誤,因爲我只在昨天開始在iOS上,但我似乎無法得到這個正確更新屏幕,計數工作正常,它的增量和檢查正確的條件只是屏幕沒有顯示,有人可以指出我在正確的方向嗎?iPhone圖像交換屏幕沒有更新

@implementation draw2D 


- (id)initWithFrame:(CGRect)frame { 
    if ((self = [super initWithFrame:frame])) { 
     // Initialization code 
     [self myTimer]; 
    } 
    return self; 
} 


// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect { 
// Drawing code 
    [self updateBackground]; 
} 

NSTimer *timer = nil; 
int count = 0; 

- (void)myTimer 
{ 
    timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateBackground) userInfo:nil repeats:NO]; 
    if (count < 1) { 
     count++; 
    } else { 
     count = 0; 
    } 


} 

- (void)updateBackground: 
{ 
    if(count == 1) 
    { 
     UIImage *myImage = [UIImage imageNamed:@"bg.png"]; 
     CGRect imageRect = CGRectMake(0, 0, 320, 480); 
     [myImage drawInRect:imageRect]; 
     [myImage release]; 
    // count++; 
    } 
    else 
     if (count == 0) 
     { 
      UIImage *myImage = [UIImage imageNamed:@"SCENE_002.png"]; 
      CGRect imageRect = CGRectMake(0, 0, 320, 480); 
      [myImage drawInRect:imageRect]; 
      [myImage release]; 
    //  count = 0; 
     } 
    [self myTimer]; 
} 
@end 

編輯:它顯示第一個圖像,但從不交換到第二個。

回答

1

第二次看,你有兩個方法調用updateBackground,一個是drawRect,它可以在你更新顯示時隨時調用,並在定時器啓動時調用。在myTimer方法中,您正在更改計數值,期望屏幕將在0.5秒後刷新。

我很懷疑一個圖像永遠不會顯示的原因是它可能正在繪製,但是對drawInRect的調用可能會觸發drawRect,然後它可以將最近繪製的圖像上的其他圖像blit。

所以,基本上我會從刪除的drawRect調用updateBackground:

**原來的答案:

[UIView的drawRect中]只有當所謂的必要。初次調用之後,UIKit只是在內部緩存結果,並繼續將緩存的副本重新繪製到顯示中。

你需要做的是發送一個信號到視圖,它需要刷新。你可以通過添加該行來實現:

[self setNeedsDisplay]; 

在你的updateBackground選擇器中。

此外,爲什麼每次啓動後都重新創建計時器,而不是將重複參數更改爲YES?

+0

我的意圖是讓計時器在視圖開始時創建並啓動。我嘗試將它放在initWithFrame方法中,但它不起作用。所以我轉向了我現在看到的這是錯誤的。我是否理解drawRect在每次屏幕刷新時都被調用?如果是這樣,我該如何獨立啓動計時器?謝謝 – Hamid 2010-11-05 08:00:05

+0

至於initWithFrame:沒有運行 - UIView子類是從NIB或XIB文件加載的嗎?如果是這樣,initWithFrame:不被調用,initWithCoder:is。蘋果從來沒有真正清楚過的討厭的小東西。 – Chris 2010-11-05 15:23:28