UIScrollView ImageView顶部有引脚

Kop*_*ppo 12 iphone mapping zooming uiscrollview

我有一个UIScrollViewUIImageView.我想在这上面显示引脚imageView.当我添加引脚作为子视图时ImageView,一切都很棒,除了缩放时,引脚上也会发生缩放转换.我不希望这种行为,并希望我的引脚保持不变.

因此,我选择将Pins添加到位于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];
Run Code Online (Sandbox Code Playgroud)

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

[iconsView addSubview:pinIcon];
Run Code Online (Sandbox Code Playgroud)

我一直试图弄清楚如何让我的引脚悬停在地图上而不会在比例发生时移动.

Dav*_*aun 9

我喜欢你将所有引脚保存在专用于引脚(iconView)的视图中的想法,但我决定只将它们添加为你所谓的imageViewMap的子视图.

将QuartzCore.framework文件导入项目.

调用视图控制器MyViewController.

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

// 等等

@implementation MyViewController

@synthesize scrollView;
@synthesize imageView;
Run Code Online (Sandbox Code Playgroud)

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

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

scrollView.delegate = self;
Run Code Online (Sandbox Code Playgroud)

使用这些委托方法:

- (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;
}
Run Code Online (Sandbox Code Playgroud)