我在ViewController中有一个ScrollView,在ScrollView上我有一个View。我使用该视图绘制像rect,circle,line,text,... 对于绘图我使用UIBezierPath。 问题是当我放大视图(即滚动视图的子视图)时,圆形,直线和矩形都是像素化的。 我使用UIScrollViewDelegate方法以这种方式进行缩放: 请记住:mapview是scrollview的子视图!!
#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
块引用