在缩放时保持UIButton相对 [英] Keeping UIButton relative when zooming
问题描述
我有一个带有UIImageview的UIScrollview,里面显示了平面图。我在scrollview中也有一个UIButton作为标记/引脚。
I have a UIScrollview with a UIImageview inside it showing a floor plan. I also have a UIButton within the scrollview that acts as a marker/pin.
我已经实现了对图像进行捏合/双击的缩放,但是UIButton元素显然没有当图像滚动和/或缩放时移动。
I have implemented zooming with pinching/double tapping on the image, but the UIButton element obviously doesn't move when the image scrolls and/or zooms.
我已尝试以下代码尝试在缩放完成时重新定位按钮:
I have tried the following code to try and resposition the button when a zoom is completed:
[myButton setFrame:CGRectMake(
(myButton.frame.origin.x - myButton.frame.size.width / 2) * _scrollView.zoomScale,
(myButton.frame.origin.y - myButton.frame.size.height / 2) * _scrollView.zoomScale,
myButton.frame.size.width, myButton.frame.size.height)];
这会将按钮移动到左上角。
This moves the button to the top left hand corner.
有没有人知道我应该做些什么来保持按钮相对于图像(就像标记在谷歌地图上的做法一样)。
Has anyone got any idea of what I should be doing to keep the button relative to the image (the same way a marker does on a Google map).
推荐答案
好的,这可以通过以下方式解决:
Ok well this was solved by the following:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"Marker.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(640.0, 230.0, 30.0, 46.0);
[button addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchUpInside];
[self.imageView addSubview: button];
self.imageView.userInteractionEnabled = YES;
基本上通过直接添加按钮到imageView然后启用userInteraction,我就可以获得所有需要的功能。
Basically by adding the button to the imageView directly and then enabling userInteraction, I was able to get all the functionality needed.
这篇关于在缩放时保持UIButton相对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!