多个图像/button相对于它们在可缩放图像上的位置保持不变

我用UIScrolView制作了一个地图,我想将其他小图像或button放到地图上,并在放大时将它们置于地图上的相对位置,并且每次都可以点击它们。 所以当缩小的时候,A国的一个button在放大时仍然会在A国,当你在放大的时候从国家视图中滚动出来的时候,屏幕就会消失。我怎么能这么做呢?

据我所知,你想在自己的自定义地图上放置自定义视图。 你需要保持相同的大小的意见,但他们应该移动,当你滚动或缩放imageView。 缩放或滚动时,必须将视图放置到scrollView的超视图并重新计算位置:

CustomMapViewController.h:

@interface CustomMapViewController : UIViewController <UIScrollViewDelegate> { UIScrollView *_scrollView; UIImageView *_mapImageView; NSArray *_customViews; } 

CustomMapViewController.m:

 #import "CustomMapViewController.h" enum { kAddContactButton = 1, kInfoDarkButton, kInfoLightButton, kLogoImage, }; @implementation CustomMapViewController - (void)dealloc { [_scrollView release]; _scrollView = nil; [_mapImageView release]; _mapImageView = nil; [_customViews release]; _customViews = nil; [super dealloc]; } - (void) loadView { [super loadView]; UIImageView *mapImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map.png"]]; UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; scrollView.delegate = self; scrollView.minimumZoomScale = 0.2; scrollView.maximumZoomScale = 2.0; [scrollView addSubview:mapImageView]; scrollView.contentSize = mapImageView.frame.size; [self.view addSubview:scrollView]; _scrollView = scrollView; _mapImageView = mapImageView; // Add custom views UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeContactAdd]; btn1.tag = kAddContactButton; [self.view addSubview:btn1]; UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeInfoDark]; btn2.tag = kInfoDarkButton; [self.view addSubview:btn2]; UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeInfoLight]; btn3.tag = kInfoLightButton; [self.view addSubview:btn3]; UIImageView *image = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]] autorelease]; image.tag = kLogoImage; [self.view addSubview:image]; _customViews = [[NSArray alloc] initWithObjects:btn1, btn2, btn3, image, nil]; [self _zoomToFit]; } - (void) _zoomToFit { UIScrollView *scrollView = _scrollView; CGFloat contentWidth = scrollView.contentSize.width; CGFloat contentHeigth = scrollView.contentSize.height; CGFloat viewWidth = scrollView.frame.size.width; CGFloat viewHeight = scrollView.frame.size.height; CGFloat width = viewWidth / contentWidth; CGFloat heigth = viewHeight / contentHeigth; CGFloat scale = MIN(width, heigth); // to fit // CGFloat scale = MAX(width, heigth); // to fill // May be should add something like this if ( scale < _scrollView.minimumZoomScale ) { _scrollView.minimumZoomScale = scale; } else if ( scale > _scrollView.maximumZoomScale ) { _scrollView.maximumZoomScale = scale; } _scrollView.zoomScale = scale; } //////////////////////////////////////////////////////////////////////////////////////// #pragma mark - Positions - (void) _updatePositionForViews:(NSArray *)views { CGFloat scale = _scrollView.zoomScale; CGPoint contentOffset = _scrollView.contentOffset; for ( UIView *view in views ) { CGPoint basePosition = [self _basePositionForView:view]; [self _updatePositionForView:view scale:scale basePosition:basePosition offset:contentOffset]; } } - (CGPoint) _basePositionForView:(UIView *)view { switch (view.tag) { case kAddContactButton: return CGPointMake(50.0, 50.0); case kInfoDarkButton: return CGPointMake(250.0, 250.0); case kInfoLightButton: return CGPointMake(450.0, 250.0); case kLogoImage: return CGPointMake(650.0, 450.0); default: return CGPointZero; } } - (void) _updatePositionForView:(UIView *)view scale:(CGFloat)scale basePosition:(CGPoint)basePosition offset:(CGPoint)offset; { CGPoint position; position.x = (basePosition.x * scale) - offset.x; position.y = (basePosition.y * scale) - offset.y; CGRect frame = view.frame; frame.origin = position; view.frame = frame; } ////////////////////////////////////////////////////////////////////////////////////// #pragma mark - UIScrollViewDelegate - (void)scrollViewDidZoom:(UIScrollView *)scrollView { [self _updatePositionForViews:_customViews]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { [self _updatePositionForViews:_customViews]; } - (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView; { return _mapImageView; } @end