【问题标题】:UIButton position while UIImage zoom on UIScrollViewUIImage 在 UIScrollView 上缩放时的 UIButton 位置
【发布时间】:2013-05-25 22:35:32
【问题描述】:

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

问题

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

TLDR, 我需要在UIScrollView 上用UIImage 重现ma​​pView with annotation kinda concept。有人可以帮忙吗?

提前致谢:)

【问题讨论】:

  • UIImageView上是否添加了UIButton?
  • @IronMan:它在 imageView 之上,但在层次结构中的 scrollView 内部
  • @Meera 你得到解决方案了吗,我也面临同样的问题。

标签: ios uiscrollview uiimageview uibutton


【解决方案1】:

我已经找到了答案。我最初将按钮值存储在 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
 }

【讨论】:

  • 这对我不起作用..当我尝试将比例应用于按钮的原点时..根据捏合速度,原点拍摄到更高的值。超出可见屏幕的范围。 :(
  • 您是否正确计算了比例因子?
  • 想通了,必须做一些数学运算和一些调试,phhewww.. :) 不过感谢您的回复。
【解决方案2】:

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

//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;

试试看,祝你好运

【讨论】:

  • :谢谢。但是如何计算mapOffsetX,mapOffsetY呢?
  • 无法从地图中找到偏移量,因为我没有使用地图。我正在使用带有按钮的滚动视图和图像。
  • 如何计算mapOffsetX、mapOffsetY和mapZoomFactor
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多