自定义绘图在退出之前不可见

发布于 2024-10-12 12:35:32 字数 1780 浏览 4 评论 0原文

在我的 UIView 子类中,自定义绘图。原始绘图工作正常,即当从 viewDidLoad 触发或从滑动或摇动手势触发时,这会清除屏幕并重新绘制。

当我尝试在已有的东西之上绘制时,问题就出现了——它的行为不稳定,通常不可见并且与我已经存在的位置偏移放置它,但是如果我按主页按钮,它就会出现。按下主页按钮后,随着视图消失,我可以瞥见我试图绘制的内容。如果我重新启动应用程序,所有绘图都在那里。什么给?

我的设置方式如下:

- (void)drawRect:(CGRect)rect; {
    cgRect = [[UIScreen mainScreen] bounds];
    cgSize = cgRect.size;
        context = UIGraphicsGetCurrentContext();

    for (int i=0; i<(cgSize.width / spacing); i++) {
        for (int j=0; j<(cgSize.height / spacing); j++) {
            [self drawObjectAtPosX:i*spacing andY:j*spacing withId:i*(cgSize.height / spacing) + j];
         }
     }

某些对象上有不可见的按钮。当按下它们时,对象将以不同的颜色重新绘制:

-(void)buttonPressed:theButton {
int which = [theButton tag];
if ([[objectArray objectAtIndex:which] shouldBeRedrawn]) {
        [self redrawObjectforID:which];
    }
}

-(void)redrawObjectforID:(int)which {
        myObject *chosenOne = [objectArray objectAtIndex:which];
        CGFloat m_x = chosenOne.x;
        CGFloat m_y = chosenOne.y;
        context = UIGraphicsGetCurrentContext();
        CGContextSetRGBStrokeColor(context, 205/255.0,173/255.0,0.0,1.0); // LINE color
        CGContextSetRGBFillColor(context, 205/255.0,197/255.0,0.0,1.0); // FILL color
        CGContextTranslateCTM(context, m_x, m_y);
        CGContextRotateCTM(context, chosenOne.rotation);
        for (int i=0; i<4; i++) {
            [self drawObjectSegmentatX:m_x andY:m_y forID:which inContext:context]; // custom drawing stuff
            CGContextRotateCTM(context, radians(90));
        } 
        CGContextRotateCTM(context, -chosenOne.rotation);
        CGContextTranslateCTM(context, -m_x, -m_y);
}

In my subclass of UIView, custom drawing. The original drawing works fine, i.e. when triggered from viewDidLoad or else from a swipe or shake gesture, which clears the screen and redraws.

The problem comes when I try to draw on top of what's already there-- it behaves erratically, usually invisible and offset from where I've placed it, but then it appears if I press the home button. On home-button press, as the view disappears I get a glimpse of what I've tried to draw. If I relaunch the app all the drawing is there. What gives?

Here's how I'm set up:

- (void)drawRect:(CGRect)rect; {
    cgRect = [[UIScreen mainScreen] bounds];
    cgSize = cgRect.size;
        context = UIGraphicsGetCurrentContext();

    for (int i=0; i<(cgSize.width / spacing); i++) {
        for (int j=0; j<(cgSize.height / spacing); j++) {
            [self drawObjectAtPosX:i*spacing andY:j*spacing withId:i*(cgSize.height / spacing) + j];
         }
     }

There are invisible buttons over certain objects. When they are pressed, the object is to be redrawn in a different color:

-(void)buttonPressed:theButton {
int which = [theButton tag];
if ([[objectArray objectAtIndex:which] shouldBeRedrawn]) {
        [self redrawObjectforID:which];
    }
}

-(void)redrawObjectforID:(int)which {
        myObject *chosenOne = [objectArray objectAtIndex:which];
        CGFloat m_x = chosenOne.x;
        CGFloat m_y = chosenOne.y;
        context = UIGraphicsGetCurrentContext();
        CGContextSetRGBStrokeColor(context, 205/255.0,173/255.0,0.0,1.0); // LINE color
        CGContextSetRGBFillColor(context, 205/255.0,197/255.0,0.0,1.0); // FILL color
        CGContextTranslateCTM(context, m_x, m_y);
        CGContextRotateCTM(context, chosenOne.rotation);
        for (int i=0; i<4; i++) {
            [self drawObjectSegmentatX:m_x andY:m_y forID:which inContext:context]; // custom drawing stuff
            CGContextRotateCTM(context, radians(90));
        } 
        CGContextRotateCTM(context, -chosenOne.rotation);
        CGContextTranslateCTM(context, -m_x, -m_y);
}

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

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

发布评论

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

评论(1

回眸一遍 2024-10-19 12:35:32

回答我自己的问题,但这是我今天学到的:

所有自定义绘图都需要在 DrawRect 中。我看到的小故障行为是视图消失或恢复调用 DrawRect 的结果。搬家了,还可以用。

自定义重画的位置仍然关闭,但这是另一个问题。

Answering my own, but here's what I learned today:

All custom drawing needs to be in DrawRect. The glitchy behavior I was seeing was the result of DrawRect being called by the view disappearing or being reinstated. Moved, and it works.

Location of the custom redraw is still off, but that's another question.

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