0

我在 ViewController 中有一个 ScrollView,在 ScrollView 上有一个 View。我使用该视图来绘制矩形、圆形、线条、文本等内容。对于绘图,我使用 UIBezierPath。问题是当我放大视图(即滚动视图的子视图)时,圆、线和矩形是像素化的。我使用 UIScrollViewDelegate 方法以这种方式进行缩放:记住:mapview 是滚动视图的子视图!

#pragma mark *** ScrollView Delegate ***

- (void)scrollViewDidScroll:(UIScrollView *)scrollview
{
    [mapview setNeedsDisplay]; // Redraw scrollview content while scrolling
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return mapview;
}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
    view.contentScaleFactor =  scale * [UIScreen mainScreen].scale;

} 

有了这段代码,我得到了这个:

(我现在无法发布图像,因为我没有足够的声誉!!)

矩形没有像素化,但圆圈和线条是的。我该如何解决?可以根据我的情况使用 UIBezierPath 吗?我想绘制矢量图形元素(线、圆、矩形)并放大绘制这些对象的视图而没有像素化!!!

我的 DrawRect 实现:

if ([type isEqualToString:@"rect"])
        {
            MAPRect *rect = [[MAPRect alloc]init];
            rect.bounds = [self getBoundsForElement:properties];
            UIBezierPath *rectPath = [rect bezierPathForDrawing];
            if (![[properties valueForKey:@"strokeColor"] isEqual:@"NSCalibratedWhiteColorSpace 0 1"])
            {
                CIColor *coreColor = [CIColor colorWithString:[properties valueForKey:@"strokeColor"]]; //representation: r0.539924 g0.611259 b1.000000 a1.000000
                UIColor *color = [UIColor colorWithCIColor:coreColor];
                [rect setStrokeColor:color];
                [[rect strokeColor] set];
            }
            else
            {
                CIColor *coreColor = [CIColor colorWithString:[properties valueForKey:@"strokeColor"]];

                [[UIColor colorWithCIColor:coreColor] set];
            }
            [rectPath stroke];
            if (![[properties valueForKey:@"fillColor"] isEqual:@"NSCalibratedWhiteColorSpace 1 1"])
            {
                CIColor *coreColor = [CIColor colorWithString:[properties valueForKey:@"fillColor"]]; //representation: r0.539924 g0.611259 b1.000000 a1.000000
                UIColor *color = [UIColor colorWithCIColor:coreColor];
                [rect setFillColor:color];
                [[rect fillColor] set];
                [rectPath fill];
            }
        }

        else if ([type isEqualToString:@"circle"])
        {
            MAPCircle *circle = [[MAPCircle alloc]init];
            //circle.bounds = [self getBoundsForElement:properties];
            circle.bounds = CGRectInset([self getBoundsForElement:properties], 5.0, 5.0);
            UIBezierPath *circlePath = [circle bezierPathForDrawing];


            if (![[properties valueForKey:@"strokeColor"] isEqual:@"NSCalibratedWhiteColorSpace 0 1"])
            {
                CIColor *coreColor = [CIColor colorWithString:[properties valueForKey:@"strokeColor"]]; //representation: r0.539924 g0.611259 b1.000000 a1.000000
                UIColor *color = [UIColor colorWithCIColor:coreColor];
                [circle setStrokeColor:color];
                [[circle strokeColor] set];
            }
            else
            {
                CIColor *coreColor = [CIColor colorWithString:[properties valueForKey:@"strokeColor"]];

                [[UIColor colorWithCIColor:coreColor] set];
            }
            [circlePath stroke];
            if (![[properties valueForKey:@"fillColor"] isEqual:@"NSCalibratedWhiteColorSpace 1 1"])
            {
                CIColor *coreColor = [CIColor colorWithString:[properties valueForKey:@"fillColor"]]; //representation: r0.539924 g0.611259 b1.000000 a1.000000
                UIColor *color = [UIColor colorWithCIColor:coreColor];
                [circle setFillColor:color];
                [[circle fillColor] set];
                [circlePath fill];
            }

        }

        else if ([type isEqualToString:@"line"])
        {
            MAPLine *line = [[MAPLine alloc]init];
            line.bounds = [self getBoundsForElement:properties];
            UIBezierPath *linePath = [line bezierPathForDrawing];
            if (![[properties valueForKey:@"strokeColor"] isEqual:@"NSCalibratedWhiteColorSpace 0 1"])
            {
                CIColor *coreColor = [CIColor colorWithString:[properties valueForKey:@"strokeColor"]]; //representation: r0.539924 g0.611259 b1.000000 a1.000000
                UIColor *color = [UIColor colorWithCIColor:coreColor];
                [line setStrokeColor:color];
                [[line strokeColor] set];
            }
            else
            {
                CIColor *coreColor = [CIColor colorWithString:[properties valueForKey:@"strokeColor"]];

                [[UIColor colorWithCIColor:coreColor] set];
            }
            [linePath stroke];
        }

        else if ([type isEqualToString:@"text"])
        {
            MAPText *text = [[MAPText alloc]init];
            text.bounds = [self getBoundsForElement:properties];
            UILabel *textfield = [text getTextField];
            [textfield setText:[properties valueForKey:@"textContent"]];
            [textfield sizeToFit];
            if (self.subviews.count == j)
            {
                [self addSubview:textfield];
            }

            j ++;

        }

其中 MAPRECT, MAPCIRCLE, MAPLINE, MAPTEXT 是我用于实现元素绘制的类,例如 MAPCIRCLE 类是这样的:

#import "MAPCircle.h"

@implementation MAPCircle

- (UIBezierPath *)bezierPathForDrawing
{

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:[self bounds]];
    [path setLineWidth:[self strokeWidth]];
    return path;

}

@end

块引用

4

0 回答 0