UIView绘制rect保留前一个绘图,但不清除view.transform [英] UIView draw rect retain's the previous drawing and does not clear on view.transform

查看:122
本文介绍了UIView绘制rect保留前一个绘图,但不清除view.transform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码在UIView中显示标记。标记显示效果很好,一旦我们尝试使用变换进行缩放和缩放UIView,即使在调用setNeedsDisplay之后,第一张图仍保持原样。

I have the following code to show marker in a UIView. The marker show's well, and once we try to pinch zoom and scale the UIView using the transform the first drawing remains as it is, even after calling setNeedsDisplay.

我的自定义UIView子类具有以下代码

My Custom UIView subclass has the following code

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGFloat w=20.0f;
    CGFloat h=8.0f;


    CGContextRef context=UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextClearRect(context,self.bounds);

    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetLineCap(context, 2.0);


    CGMutablePathRef leftMarker=CGPathCreateMutable();
    CGPathMoveToPoint(leftMarker, NULL, 0, 0);
    CGPathAddLineToPoint(leftMarker, NULL, w, 0);
    CGPathAddLineToPoint(leftMarker,NULL, w, h);
    CGPathAddLineToPoint(leftMarker,NULL, h, h);
    CGPathAddLineToPoint(leftMarker,NULL,h, w);
    CGPathAddLineToPoint(leftMarker,NULL,0, w);
    CGPathAddLineToPoint(leftMarker,NULL, 0, 0);

    CGContextAddPath(context, leftMarker);

    CGContextDrawPath(context, kCGPathFill);
    const CGAffineTransform rightMarkerTransform=CGAffineTransformMakeRotateTranslate(DEGREES_TO_RADIANS(90),self.frame.size.width,0);

    CGPathRef rightMarker=CGPathCreateCopyByTransformingPath(path, &rightMarkerTransform);
    CGContextAddPath(context, rightMarker);
    CGContextDrawPath(context, kCGPathFill);
    const CGAffineTransform leftMarkerBottomTransform=CGAffineTransformMakeRotateTranslate(DEGREES_TO_RADIANS(270),0,self.frame.size.height);

    CGPathRef leftMarkerbottom=CGPathCreateCopyByTransformingPath(path, &leftMarkerBottomTransform);
    CGContextAddPath(context, leftMarkerbottom);
    CGContextDrawPath(context, kCGPathFill);

    const CGAffineTransform rightMarkerBottomTransform=CGAffineTransformMakeRotateTranslate(DEGREES_TO_RADIANS(180),self.frame.size.width,self.frame.size.height);

    CGPathRef rightMarkerBottom=CGPathCreateCopyByTransformingPath(path, &rightMarkerBottomTransform);
    CGContextAddPath(context, rightMarkerBottom);
    CGContextDrawPath(context, kCGPathFill);

    CGPathRelease(rightMarker);
    CGPathRelease(leftMarkerbottom);
    CGPathRelease(rightMarkerBottom);
    CGPathRelease(leftMarker);
}

下面列出了缩放变焦代码

The pinch zoom code is listed below

CGFloat lastScale;
-(void) handlepinchGesture:(UIPinchGestureRecognizer*)gesture{
    UIView *gestureV=gesture.view;
    CGFloat scale=gesture.scale;
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
            if(lastScale<1.0){
            lastScale=1.0;
            }
            scale=lastScale;
            break;
        default:
            break;
    }

    if(scale<1.0){
        scale=1.0;
    }
    lastScale=scale;
    gestureV.transform=CGAffineTransformMakeScale(scale, scale);
    //Even this does not work ….[gestureV setNeedsDisplay];
    gesture.scale=scale;
}


推荐答案

确定有此设置(但它应默认为YES)。

Make you sure have this set (but it should be defaulted to YES).

self.clearsContextBeforeDrawing = YES;

来自Apple的文档 UIView

From Apple's Docs in UIView



设置为YES,绘图缓冲区在调用drawRect:方法之前自动清除为透明的
black。此行为确保
在重绘视图的内容
时没有遗留任何视觉工件。如果视图的opaque属性也设置为YES,则视图的
backgroundColor属性不能为nil,否则可能会出现绘图错误
。此属性的默认值为YES。

When set to YES, the drawing buffer is automatically cleared to transparent black before the drawRect: method is called. This behavior ensures that there are no visual artifacts left over when the view’s contents are redrawn. If the view’s opaque property is also set to YES, the backgroundColor property of the view must not be nil or drawing errors may occur. The default value of this property is YES.

如果将此属性的值设置为NO,则负责
,确保视图的内容为在drawRect中正确绘制:
方法。如果您的绘图代码已经过大幅优化,设置
此属性为NO可以提高性能,尤其是在
滚动时,只需要重绘一部分视图。

If you set the value of this property to NO, you are responsible for ensuring the contents of the view are drawn properly in your drawRect: method. If your drawing code is already heavily optimized, setting this property is NO can improve performance, especially during scrolling when only a portion of the view might need to be redrawn.

这篇关于UIView绘制rect保留前一个绘图,但不清除view.transform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆