UIView drawRect中的偶数和奇数缓冲区?

时间:2011-01-22 22:25:01

标签: iphone uiview drawrect

再次问好(感谢大家以前的宝贵帮助)。

新问题:我正在进行物理模拟,但遇到了玩具版本的问题。它只需在NSStimer timer调用tick方法时在iPhone /触摸屏上绘制一个随机矩形。我已通过int frameCount将这些调用编入索引。人们看到的是逐渐建立的两组图像:一组用于偶数帧,另一组用于奇数帧。我通过将frameCount绘制到屏幕上来验证这一点,其中偶数和奇数计数位于略有不同的位置。因此,人们看到计数与帧中的图像同步地来回闪烁。缩写代码如下。我很感激您的任何建议。在我看来,必须有两个屏幕外缓冲区。但是我在这里很黑暗:-)即使这是真的,我也不知道如何合并它们,或者将它们复制到另一个。

#define MAXFRAMES 1000

// FooBar的实现的相关部分,UIView的子类:

 - (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        // Initialization code
        // Start a timer that will call the tick method of this class
        // 30 times per second  -- slowed way down for diagnostics

        timer = [NSTimer scheduledTimerWithTimeInterval:(3.0/1.0)
                    target:self selector:@selector(tick) userInfo:nil repeats:YES];

        frameCount = 0;
        self.clearsContextBeforeDrawing = NO;
        NSLog(@"frame initialized");
    }
    return self;
}

- (void)tick
{    
    // Tell the view that it needs to re-draw itself
    if (frameCount < MAXFRAMES) {
        [self setNeedsDisplay];
        frameCount++;
    }   
}


- (void)drawRect:(CGRect)rect
{

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // Draw a random rectangle with CGContextFillRect 
    // after calling CGContextSetRGBFillColor
    // Both called with suitable random parameters  

    // Draw the frameCount for diagnostic purposes

}

1 个答案:

答案 0 :(得分:0)

未指定UIView可能在其绘图层中使用的缓冲区数。如果您想要复制图纸,您应该从您自己的CG绘图上下文中绘制,然后使用或将结果绘制成可用作视图的CALayer内容的图像。