当UIImage放大UIScrollView时UIButton的位置

时间:2013-05-30 11:13:48

标签: ios uiscrollview uiimageview uibutton

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

问题

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

TLDR 下, 我需要在UIScrollView上复制带有UIImage mapView with annotation kinda concept 。任何人都可以帮忙吗?

提前致谢:)

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分: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;

试试,祝你好运