gpt4 book ai didi

iphone - UIScrollView ImageView 顶部有针脚

转载 作者:行者123 更新时间:2023-12-03 18:27:21 25 4
gpt4 key购买 nike

我有一个UIScrollView,它有一个UIImageView。我想在这个imageView上显示图钉。当我将图钉添加为 ImageView 的 subview 时,一切都很好,除了缩放时图钉上也会发生比例变换。我不希望出现这种行为,并希望我的引脚保持不变。

因此,我选择将 Pins 添加到另一个 View ,该 View 位于 ImageView 之上,也是 UIScrollView 的 subview 。如果您能想象,这里的想法是有一个图层悬停在 map 上,并且不会缩放,但会在我绘制它们的位置显示图钉。

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

基本上,这是带有 Pin 图的地点的自定义 map 。我试图让 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];

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

最佳答案

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

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

调用您的 View Controller MyViewController。

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

//等等

@implementation MyViewController

@synthesize scrollView;
@synthesize imageView;

我假设您有一个 pin View 或名为 DropPinViewController 的 View Controller 。如果你只是使用图像,它可以是 UIImageView,但我的引脚有标签,所以我使用了 xib。引脚应指向 xib 的底部中心,因为我们将在那里放置一个 anchor 。

在 MyViewController 的 viewDidLoad 中,将 pin View 添加为 imageView 的 subview 。将 imageView 作为 subview 添加到 ScrollView 中。设置scrollView的内容大小。

scrollView.delegate = self;

使用这些委托(delegate)方法:

- (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;
}

关于iphone - UIScrollView ImageView 顶部有针脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1051912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com