OGeek|极客世界-中国程序员成长平台

标题: ios - 在 for 循环中调用 setNeedsDisplay [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:20
标题: ios - 在 for 循环中调用 setNeedsDisplay

我有一些绘图代码尝试在 UIView 顶部绘制图层并显示图像。 for 循环代码每次准备要显示的部分时都会调用 setNeedsDisplay 的更新,但 View 上的 drawRect 只会在最后调用。循环是否过快并中断了之前的调用?

for ( int i = 0; i < bitmapLength; i++ ) {

      if ((tfx > 0) && (tfy > 0)) {
                updateCount++;
                sourceRect = CGRectMake(sx*8, ((((ipegHeight-1)/8)-sy)*8) + osy, tfx, tfy);
                targetRect = CGRectMake(tx*8, ty*8, tfx, tfy);
                ipegSection = CGImageCreateWithImageInRect(notJPEG, sourceRect);

                // create a layer and draw section onto it         
                drawLayer = CGLayerCreateWithContext(ctxImage, targetRect.size, nil);
                layerContext = CGLayerGetContext(drawLayer);
                CGContextDrawImage(layerContext,targetRect,ipegSection);

                // draw the layer onto the UIViews context
                CGContextDrawLayerAtPoint(currentScreen, targetRect.origin, drawLayer);


                CGImageRelease(ipegSection);

                    // Update the UI
                    [targetView setNeedsDisplay];
            }
}



Best Answer-推荐答案


drawRect on the view only gets called at the end

更具体地说,它仅在您的方法结束并且主事件循环接管时才被调用。

这正是它应该工作的方式:setNeedsDisplay 就像“轻拍肩膀”一样,它告诉事件循环在有机会时重新绘制 View 。无论您调用多少次,在您的代码完成工作之前都不会发生重绘。

长话短说,您应该只在循环结束后调用一次 setNeedsDisplay

关于ios - 在 for 循环中调用 setNeedsDisplay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28585393/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4