【问题标题】:Subclassing UIView in a map to draw lines在地图中子类化 UIView 以绘制线条
【发布时间】:2011-05-18 02:34:26
【问题描述】:

我有一个显示自定义地图的应用。我使用 CATiledView 来显示地图。

我希望能够在地图顶部绘制路线。为此,我正在创建一个 UIView,然后在添加平铺层后将其添加到 scrollView,如下所示:

- (void)displayTiledImageNamed:(NSString *)imageName size:(CGSize)imageSize
{
    // clear the previous imageView
    [imageView removeFromSuperview];
    [imageView release];
    imageView = nil;
    [linesView removeFromSuperview];
    [linesView release];
    linesView = nil;

    // reset our zoomScale to 1.0 before doing any further calculations
    self.zoomScale = 1.0;

    // make a new TilingView for the new image
    imageView = [[TilingView alloc] initWithImageName:imageName size:imageSize];
    linesView = [[LinesView alloc]initWithImageName:imageName size:imageSize];

    linesView.backgroundColor = [UIColor clearColor];
    [self addSubview:imageView];
    [self addSubview:linesView];
    [self configureForImageSize:imageSize];


}

问题是我在linesView 中创建的线没有正确缩放。

这很难描述,但是正在创建的线是按比例绘制的,就好像它是在设备本身上绘制的,而不是在地图上绘制的。见以下代码:

#import "LinesView.h"


@implementation LinesView
@synthesize imageName;


- (id)initWithImageName:(NSString *)name size:(CGSize)size
{
    if ((self = [super initWithFrame:CGRectMake(0, 0, size.width, size.height)])) {
        self.imageName = name;

    }

    return self;
}
- (void)dealloc
{
    [super dealloc];
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
    CGContextSetLineWidth(context, 20.0);
    CGContextMoveToPoint(context, 1.0f, 220.0f);
    CGContextAddLineToPoint(context, 340.0f, 80);
    CGContextStrokePath(context);
}

@end

我尝试将用于绘制线条的代码放入 tilingView 的 drawRect 方法中,并且效果很好。线宽相对于地图为 20px。在linesView 中,该行相对于设备显示为20px 宽,并且相对于滚动视图定位。

对不起,我正在尽力描述问题...

【问题讨论】:

    标签: iphone uiview ios4 uiscrollview subclass


    【解决方案1】:

    想通了 - 我将 linesView 添加到 tilingView 而不是这样的 scrollView:

    - (void)displayTiledImageNamed:(NSString *)imageName size:(CGSize)imageSize
    {
        // clear the previous imageView
        [imageView removeFromSuperview];
        [imageView release];
        imageView = nil;
        [linesView removeFromSuperview];
        [linesView release];
        linesView = nil;
    
        // reset our zoomScale to 1.0 before doing any further calculations
        self.zoomScale = 1.0;
    
        // make a new TilingView for the new image
        imageView = [[TilingView alloc] initWithImageName:imageName size:imageSize];
        linesView = [[LinesView alloc]initWithImageName:imageName size:imageSize];
    
        linesView.backgroundColor = [UIColor clearColor];
        [self addSubview:imageView];
        [imageView addSubview:linesView];
        [self configureForImageSize:imageSize];
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      相关资源
      最近更新 更多