iPhone 图像交换屏幕未更新

发布于 2024-10-01 04:03:56 字数 1402 浏览 0 评论 0原文

我知道这里很可能存在严重错误,因为我昨天才开始使用 iOS,但我似乎无法正确更新屏幕,计数工作正常,并且它正在递增并正确检查条件,只是屏幕没有这样做。没有显示,有人可以指出我正确的方向吗?

@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

编辑:它显示第一个图像,但从不交换到第二个图像。

I know there is most probably something seriously wrong here because I only started on iOS yesterday but I can't seem to get this to update the screen properly, the count works fine and it's incrementing and checking the conditions correctly just that the screen doesn't get displayed, can anybody point me in the right direction please?

@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

EDIT: It displays the first image, but never swaps to the second.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画中仙 2024-10-08 04:03:56

第二次看时,您有两个调用 updateBackground 的方法,一个是drawRect,它可以在您更新显示时以及在计时器触发时随时调用。在 myTimer 方法中,您正在更改计数值,预计屏幕将在 0.5 秒后刷新。

我怀疑一张图像从未显示的原因是它可能正在被绘制,但对drawInRect的调用可能会触发drawRect,然后它可能会在最近绘制的图像之上位块传输其他图像。

所以,基本上我会从 drawRect 中删除对 updateBackground 的调用:

** 原始答案:

仅在必要时调用 [UIView drawRect] 。在初始调用之后,UIKit 只是在内部缓存结果,并继续将缓存的副本重绘到显示器上。

您需要做的就是向视图发送需要刷新的信号。您可以通过添加以下行来做到这一点:

[self setNeedsDisplay];

在 updateBackground 选择器中。

另外,为什么每次触发后都要重新创建计时器,而不是将重复参数更改为 YES?

On second look, you have two things methods calling updateBackground, one is drawRect, which can be called any time you're updating the display, and on the timer fire. In the myTimer method, you're changing the count value, with the expectation the screen will refresh 0.5 seconds later.

I'm suspicious that the reason one image is never displayed is that it may be being drawn, but the call to drawInRect might fire drawRect, which could then blit the other image on top of the recently drawn image.

So, basically I'd remove the call to updateBackground from drawRect:

** ORIGINAL ANSWER:

[UIView drawRect] is only called when necessary. After the initial call, UIKit just caches the result internally and continues to redraw the cached copy to the display.

What you need to do is send a signal to the view that it needs to be refresh. You can do that by adding the line:

[self setNeedsDisplay];

In your updateBackground selector.

Also, why are you re-creating the timer after each time it fires, instead of changing the repeats parameter to YES?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文