2009-06-27 72 views
11

我有一个UIScrollView它有一个UIImageView。我想在此imageView上显示引脚。当我将引脚添加为ImageView的子视图时,除了缩放引脚上的缩放变换时,一切都非常棒。我不希望这种行为,并希望我的针脚保持不变。UIScrollView ImageView顶部针脚

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

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

基本上这是一个带有引脚的地方的自定义地图。我正在尝试让引脚浮起来,而不是放大和缩小我的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]; 

代码稍后添加引脚上的一些事件。

[iconsView addSubview:pinIcon]; 

我被困在试图TP找出如何让我的脚在地图上悬停不动,当规模发生了。

回答

9

我喜欢将所有引脚保留在专用于引脚(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; 

我假设你有一个销视图或称为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; 
} 
+0

这太棒了我有点生病。我有许多其他令人费解的方法可以做到这一点...非常简单。我切换到这个,它很好。 – malaki1974 2013-07-03 18:42:20

3

因此,我已经实施的一件事尚未解决我的问题,但我认为这是正确的道路是从这篇文章。

Anchor a UIView

我有我的看法层次结构,如下。

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

下一步,我需要解决的是让我的脚加入到IconsView在同一地点停泊时UIImageView被放大和缩小。

我有一个for循环,通过我所有的销,它们和IconsView子视图,并更新了自己的原点X Y.这是在UIScrollView代表的scrollViewDidScroll方法去。

问题是这种技术在设备上的可怕表现,因为当地图上有很多引脚时,需要发生太多的处理。

0

几个建议:

1)你会因为它限制了它试图通过默认显示的引脚数的谷歌地图应用程序通知。即使有更多可能的匹配,它也只会显示最接近中心点的结果。

2)您可以在任何滚动或缩放操作期间暂时隐藏引脚,以便界面保持响应并在图像视图停止移动时重新绘制它们。3)如果您必须显示大量的针脚并为它们设置动画,您还可以考虑使用CABasicAnimations同时为针脚和背景设置动画,使用变换背景的相同数学转换针脚位置规模。这可以通过更平滑地扩展动画并在相当低的水平上运行动画来平滑显示,但会引入其他挑战和局限。

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

巴尼

0

我有同样的问题,我觉得手动校正针的位置不是很漂亮的想法。然后,我宁愿将我的针脚拖到代表函数viewForZoomingInScrollView:中返回的视图中,并在缩放完成后更新子视图(标记)大小。

但是,它让我感到这在Google地图应用中是可行的,但我无法重新创建它。

还有一件事: 任何人都可以解释我行为展览当我添加子视图到UIScrollView不是'viewForZoomingInScrollView'..?例如:

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

对于初始视图和平移时,这似乎工作正常。缩放时,这些子视图不会调整大小,这实际上是我们的目标,但问题在于UIView框架原点以某种相当奇怪的方式在滚动视图中移动。看起来,如果放大,marker.frame.size.origin总是朝着左上角(超级观点的(0,0))移动,产生我们实际上缩小的印象。这也很奇怪,因为“缩放中心”应该始终位于屏幕的中心,不是吗?好吧,我现在不明白。

凹凸。 ;)

0

感谢David百灵。你的代码除了我不得不注释掉的一行之外。我的项目有一个UIScrollView,然后是一个显示地图的UIImageView,最后是检测到双击手势后代码定位的引脚(按钮)。你的代码帮助我考虑了缩放比例。虽然限制,当我捏缩放,每个别针不“粘”到绘图的特定部分,我双击。当我注释掉这一行时,我偶然发现了解决方案:

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

我没有完整的理解,但我的代码现在可以工作!我希望别人能从我的经历中受益。谢谢大卫!