UIScrollView map with pins that stay at a fixed size?
In my app, I have a ScrollView that shows a map(just a jpeg). On top of the ScrollView, I added some pins(UIImageView) So far so good.
But when I zoom in, the pins also get larger. I would like the pins to st开发者_如何学Goay at a fixed size, just like the pins on the google map application on the iPhone.
How do I solve this?
Thanks!
I've been looking into this for a few days and I've come to the conclusion that there is currently no good way to do this.
First, put your pins as a direct subview of the UIScrollView. Now they'll stay the same size, but will move around when you zoom.
In OS version >=3.2 in UIScrollViewDelegate there is -scrollViewDidZoom:, where you can update the pins to the right coords as it scrolls, and this works great. But in 3.1 we're out of luck. I tried having a NSTimer going and updating the coords instead, but it's not performant on the device.
As a fallback what I do is hide the pins when zooming starts (viewForZoomingInScrollView) and when zooming ends (scrollViewDidEndZooming) update their coords and unhide them.
Make a container UIView that is the same size as the map. Add the map to the UIView with autoresizing of flexible width and height. Add all the pins as individual images, each with flexible margins. Use the container as the zooming view.
If the system autoresizeing does not retain the margin ratio then you can implement layoutSubviews in a custom container view.
精彩评论