12

我有一个UIScrollView其中有一个UIImageView. 我想在这个上显示别针imageView。当我将引脚添加为 的子视图时ImageView,一切都很好,除了当您缩放时,引脚上也会发生比例变换。我不希望这种行为,并希望我的别针保持不变。

所以我选择将 Pin 图添加到位于 ImageView 顶部的另一个视图,也是UIScrollView. 如果您可以想象,这里的想法是有一个悬停在地图上并且不会缩放但在我绘制它们的位置显示图钉的图层。

添加到图层视图时的图钉不会ImageView缩放。然而,问题就变成了引脚的位置与原始原点 x/y 不匹配,因为ImageView已经进行了比例变换。

基本上,这是一个带有 Pins 的地方的自定义地图。我试图让 Pins 浮在上面,而不是在我的 ImageView 上放大和缩小,但还记得在缩放发生时我放置它们的位置。

一些代码:

scrollView = [[UIScrollView alloc] initWithFrame:viewRect];

scrollView.delegate = self;
scrollView.pagingEnabled = NO;
scrollView.scrollsToTop = NO;
[scrollView setBackgroundColor:[UIColor clearColor]];
scrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView.bounces = YES;
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;

imageViewMap = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];

imageViewMap.userInteractionEnabled = YES;

viewRect = CGRectMake(0,0,imageViewMap.image.size.width,imageViewMap.image.size.height);

//viewRect = CGRectMake(0,0,2976,3928);

[scrollView addSubview:imageViewMap];

[scrollView setContentSize:CGSizeMake(viewRect.size.width, viewRect.size.height)];

iconsView = [[UIView alloc] initWithFrame:imageViewMap.frame];

[scrollView addSubview:iconsView];

稍后在某些事件上添加 Pin 的代码。

[iconsView addSubview:pinIcon];

我一直在试图弄清楚如何让我的图钉在地图上悬停而不移动,当比例发生时。

4

5 回答 5

10

我喜欢您将所有图钉保留在专用于图钉(iconsView)的视图中的想法,但我决定将它们添加为您所谓的 imageViewMap 的子视图。

将 QuartzCore.framework 文件导入您的项目。

调用您的视图控制器 MyViewController。

#import <QuartzCore/QuartzCore.h>
@interface MyViewController : UIViewController <UIScrollViewDelegate>
@property (retain) UIScrollView *scrollView;
@property (retain) UIImageView *imageView;

// 等等

@implementation MyViewController

@synthesize scrollView;
@synthesize imageView;

我假设您有一个 pin 视图或称为 DropPinViewController 的视图控制器。如果您只是使用图像,它可以是 UIImageView,但我的引脚有标签,所以我使用了 xib。大头针应该指向 xib 的底部中心,因为我们要在那里放置一个锚点。

在 MyViewController 的 viewDidLoad 中,将 pin 视图添加为 imageView 的子视图。将 imageView 作为子视图添加到 scrollView。设置scrollView的内容大小。

scrollView.delegate = self;

使用这些委托方法:

- (void)scrollViewDidZoom:(UIScrollView *)aScrollView
{   
    for (UIView *dropPinView in imageView.subviews) {       
    CGRect oldFrame = dropPinView.frame;
    // 0.5 means the anchor is centered on the x axis. 1 means the anchor is at the bottom of the view. If you comment out this line, the pin's center will stay where it is regardless of how much you zoom. I have it so that the bottom of the pin stays fixed. This should help user RomeoF.
   [dropPinView.layer setAnchorPoint:CGPointMake(0.5, 1)];
    dropPinView.frame = oldFrame;
    // When you zoom in on scrollView, it gets a larger zoom scale value.
    // You transform the pin by scaling it by the inverse of this value.
    dropPinView.transform = CGAffineTransformMakeScale(1.0/scrollView.zoomScale, 1.0/scrollView.zoomScale);
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return imageView;
}
于 2011-12-02T03:25:55.757 回答
3

因此,我实施的一件事并没有解决我的问题,但我认为这是正确的道路,来自这篇文章。

锚定一个 UIView

我的视图层次结构如下。

UIScrollView
 - UIImageView (map image)
 - IconsView (layer image to hold icons)

我需要解决的下一步是在放大和缩小IconsView时将我的图钉添加到固定在同一位置。UIImageView

我有一个 for 循环,它遍历了我所有的子视图,IconsView并更新了它们的 Origin X 和 Y。这是UIScrollView代表的scrollViewDidScroll方法。

问题是这种技术在设备上的性能很糟糕,因为当地图上有很多大头针时,需要进行太多处理。

于 2009-06-28T01:27:57.177 回答
0

一些建议:

1) 您会在 Google 地图应用程序中注意到,它会限制默认情况下尝试显示的图钉数量。即使有更多可能的匹配项,它也只会显示最接近中心点的结果。

2)您可以在任何滚动或捏合操作期间短暂隐藏引脚,以便界面保持响应并在图像视图停止移动时重新绘制它们。

3) 如果您必须显示大量引脚并为其设置动画,您还可以考虑使用 CABasicAnimations 同时为引脚和背景设置动画,使用转换背景比例的相同数学来转换引脚位置。这可能会通过更平滑地传播动画并在相当低的级别上运行它们来平滑显示,但会引入其他挑战和限制。

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Animation_Types_Timing/Articles/PropertyAnimations.html#//apple_ref/doc/uid/TP40006672-SW1

巴尼

于 2009-07-08T11:13:41.273 回答
0

我有同样的问题,我发现手动更正引脚位置的想法不是很漂亮。然后,我宁愿将我的引脚绘制到委托函数中返回的视图上,viewForZoomingInScrollView:并在缩放完成后更新子视图(标记)大小。

然而,它让我觉得这在谷歌地图应用程序中是可能的,但我无法重新创建它。

还有一件事:当我向 UIScrollView 添加不是“viewForZoomingInScrollView”的子视图时,谁能解释我的行为表现?例如:

UIImageView* marker = [[UIImageView alloc] initWithImage: myMarkerImage];
[marker setCenter: CGPointMake(250,150)];
[myScrollView addSubview: marker];

这似乎适用于初始视图和平移时。缩放时,这些子视图不会调整大小,这实际上是我们的目标,但问题是 UIView 框架原点以某种相当奇怪的方式在滚动视图中移动。看起来,如果放大,marker.frame.size.origin总是向左上角移动(superview 的 (0,0)),给人一种我们实际上是在缩小的印象。这也很奇怪,因为“缩放中心”应该总是在屏幕的中心,不是吗?嗯,我暂时不明白。

撞。;)

于 2010-04-13T01:08:03.460 回答
0

感谢大卫布劳恩。您的代码有效,除了我必须注释掉的一行。我的项目涉及有一个 UIScrollView,然后是一个 UIImageView 在顶部显示地图,最后是在感应到双击手势后代码定位的引脚(按钮)。您的代码帮助我解释了缩放比例。但限制是当我捏缩放时,每个图钉都不会“粘”到我双击的绘图的特定部分。当我注释掉这一行时,我偶然发现了解决方案:

//[dropPinView.layer setAnchorPoint:CGPointMake(0.5, 1)];

我没有完全理解,但我的代码现在可以工作了!我希望其他人能从我的经验中受益。谢谢大卫!

于 2012-02-01T16:15:55.723 回答