我知道這裏最有可能是嚴重錯誤,因爲我只在昨天開始在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
編輯:它顯示第一個圖像,但從不交換到第二個。
我的意圖是讓計時器在視圖開始時創建並啓動。我嘗試將它放在initWithFrame方法中,但它不起作用。所以我轉向了我現在看到的這是錯誤的。我是否理解drawRect在每次屏幕刷新時都被調用?如果是這樣,我該如何獨立啓動計時器?謝謝 – Hamid 2010-11-05 08:00:05
至於initWithFrame:沒有運行 - UIView子類是從NIB或XIB文件加載的嗎?如果是這樣,initWithFrame:不被調用,initWithCoder:is。蘋果從來沒有真正清楚過的討厭的小東西。 – Chris 2010-11-05 15:23:28