2

我有一个场景,我需要实现一个离线地图概念,为此我UIScrollView在 PinchGesture 上使用地图图像,效果很好。

问题

我有一个UIButton地图。缩放时,按钮不会跟踪其相对于UIImageView正在缩放的​​位置。我可以重新构建按钮而不影响其大小。但立场不对。

TLDR,我需要在上面复制带有注释有点概念的mapView 。任何人都可以帮忙吗?UIScrollViewUIImage

提前致谢 :)

4

2 回答 2

2

我已经找到了答案。我最初将按钮值存储在 CGRect initialButtonFrame 中。然后我使用滚动视图委托更新了按钮框架(只有原点,而不是按钮大小的大小,因为我希望按钮不像图像那样缩放,即;我的按钮不应该缩放)

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
   [self manageImageOnScrollView];//here i managed the image's coordinates and zoom
   [self manageButtonCoordinatesWithRespectToImageWithScale:scale];
}

-(void)manageButtonCoordinatesWithRespectToImageWithScale:(float)scaleFactor
{

//initialButtonFrame is frame of button 
   self.button.frame = CGRectMake((initialButtonFrame.origin.x * scaleFactor),
                                  (initialButtonFrame.origin.y * scaleFactor),
                                           initialButtonFrame.size.width,
                                           initialButtonFrame.size.height);

   [self.scrollView addSubview:self.button];// I removed the button from superview while zooming and later added with updated button coordinates which I got here
 }
于 2013-05-31T07:24:53.433 回答
0

如果您知道地图的当前偏移和缩放,您应该能够计算按钮的位置:

//Assuming your map image has its origin at 0, 0
CGPoint mapOffsetX, mapOffsetY; // these would come from your map as you calculated it.
CGPoint mapZoomFactor; // 1.0 means not zoomed, 3.0 means zooming in 3x, etc
CGPoint buttonAnchorPosition; //the position of your button on your map at 1.0 zoom

CGFloat buttonX = buttonAnchorPosition.x * mapZoomFactor + mapOffsetX;
CGFloat buttonY = buttonAnchorPosition.y * mapZoomFactor + mapOffsetY;

CGPoint buttonPosition = CGPointMake(buttonX, buttonY);

button.position = buttonPosition;

试试看,祝你好运

于 2013-05-30T11:35:04.930 回答