ViewController に ScrollView があり、ScrollView に View があります。そのビューを使用して、四角形、円、線、テキストなどを描画します...描画には、UIBezierPath を使用します。問題は、ビュー(スクロールビューのサブビュー)をズームすると、円、線、四角形がピクセル化されることです。この方法でズームするために UIScrollViewDelegate メソッドを使用します。
#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、MAPIRCLE、MAPLINE、MAPTEXT は、要素の描画を実装するために使用したクラスです。たとえば、MAPIRCLE クラスは次のようになります。
#import "MAPCircle.h"
@implementation MAPCircle
- (UIBezierPath *)bezierPathForDrawing
{
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:[self bounds]];
[path setLineWidth:[self strokeWidth]];
return path;
}
@end
引用符