string under Map Kit pin (mkannotationview image) - swift

I am trying to set names under pins that are on a map. Each name should be specific.
I am not using default pins, hence each pin is a specific image.
I was thinking of generating some kind of image that would present the image of the pin and the name as a label under it. Then append the whole thing as the image of the mkannotationview. But this looks like a mess to me.
Is there a way I could possibly append a label under a mkannotationview?
Or should a make a custom mkannotationview?
Thank you for your time.

Starting with iOS 11, if you subclass MKMarkerAnnotationView, you get those texts below the bubble for free, including collition detections: the text is sometimes rendered more left or right, depending where there is some space left on the map.

The MKAnnotationView class is a subclass of UIView, so you should be able to subclass it fairly easily.
I would probably create a custom subclass, and have my subclass add a label as a subview of the MKAnnotationView. You might need to adjust the frame of the view to make room for the label you're adding.

Related

Get a custom UIView subclass to follow a UIControl around

I've got a subclass of UIControl which represents a slider switch, with a variable 'percent' which stores the current position of the slider, i.e. 0.0 means the slider is on the left, 1.0 means the slider is on the right. I have two images which look like speech bubbles, and when the user clicks the slider I want to display one of these images (they are the same size but look different, designed so one is displayed if percent <= 0.5 and the other one if percent > 0.5) and have the 'tail' of the speech bubble follow the slider (i.e. the frame is set as a function of the percentage).
I have created a custom UIView for the image which changes the image to display via a boolean parameter. I have tried creating a parameter in the UIControl subclass that draws the UIView along with all the other drawing, but I need to draw it outside the UIControl's rect, which can't be done from inside the UIControl's drawRect: method. What would be a good way of managing these two views and making sure the speech bubble always updates when the percentage value of the UIControl changes?
Sounds like your UIControl should simply be:
Tall enough to contain the bar and bubble view
Transparent (so that you can see behind the part of the view where the bubble "slides")
Or am I missing something?
First off, make sure the continuous property is set to TRUE in your UISlider.
Then, make sure you have an IBAction in place to receive update events from your UISlider. You can make the connection easily in Interface Builder (built into XCode 4) or you can do it programatically using UIControl's addTarget:action:forControlEvents: method.
And lastly, inside that IBAction you can now change the frame of your "speech bubble" UIView (make sure to link that up to your parent view controller as a member in your #interface .h file). Where it appears in relation to the slider is left to you as a homework assignment (cause it's really implementation specific -- or, to put it another way, how it appears is up to each individual app & programmer).
I hope this information helps you out!
Thanks for the answers - it turned out the best way to do it was to declare a delegate protocol for my custom UIControl, and assign the speech bubble view as the delegate. Then I made a method that tells the speech bubble to update location (passing in the location) and called that from the UIControl whenever the position of the control updated.
Probably the simplier way is to add to the UIControl a property pointing to the UIView and move it around in the method(s) where you change the control state. Of course to do that you need to create a class like MyCustomControl that extends UIControl.

Custom annotation view using MKCircle

I am trying to make a custom view for a custom MKAnnotation I have. What I want is a solid circle with a number inside, and a colour depending on what the value of that number is. The number's value will be taken from my custom MKAnnotation class. I currently have the annotation view as a map pin, and I have a callout method assigned to it etc. so I want my new view to function the same as this pin view. How would I go about this? I can't seem to find anything that teaches me how to do this: would I have to create the circle as an overlay and handle the drawing in the map view controller?

iPhone SDK: Adding text to a MKMapView

I just want to add some text to a MKMapView (dynamically in code). It seems like one way to go would be subclassing MKShape and defining a custom annotation (i.e. one without a pin or any other graphics) but I'm uncertain how to do it. Also, it seems like it might be possible to layer another control on top of the MKMapView that would allow me to add text.
Why not just add a UILabel as a subview of the map?

MKAnnotationView in both hovering and pinned states

I'm trying to add a pin (MKAnnotation and MKAnnotationView) to my MKMapView and allow the user to drag it around.
I'd also like to make the dragging of the pin animated and interactive like the iPhone's Map App.
My question is how do I change the state of the MKAnnoationView so that it's hovering over the map (so the pin isn't actually inside the map)?
I'm not 100% sure how to do this.
At present, my colleague as found an hovering image that he swaps with the default MKAnnotationView, but that means I can't easily animate between the two.
Not sure what you exactly want to do but I have used Apple's example in the iPhone App Programming Guide (Handling Events in an Annotation View) to implement the draggable pin.
It has a partial code but tha may be enough for you to figure it out.
Basically, you must subclass the MKAnnnotation and MKPinAnnotationView and in your CustomAnnotationView class you have to implement delegate methods to handle touch events, as shown in the Apple example.
There was a bit of filling out or modification needed because the code snippet was not complete, but I have reproduced the behaviour of the pin on the Apple's iPhone Map app exactly (except that I did not implement the right accessory button).
In it, the pin feels like it is hovering. So, I suspect that you have no need for the hovering image you have mentioned.
I also presume that by providing a BOOL property, you could make the pin draggable or "fixed" programmatically.
Does this help?

iPhone dev: adding an overlapping label to the image

I'm trying to figure out a best way to implement the picture-editing capability shown in the native address book app on iPhone.
On the built-in address book, this is how the picture looks like before editing:
qkpic.com/2f7d4 http://qkpic.com/2f7d4
And after clicking edit, notice how "Edit" overlay is added and the image becomes clickable:
qkpic.com/fb2f4 http://qkpic.com/fb2f4
What would be the best way to implement something like this? Should I make the image a button from the beginning and have tapping disabled at first? If so, what steps are required to add an overlay/label to the image (in above example, gray border + the text "Edit" is added)
The easiest way is to use Interface Builder, create a container view, then add a UIImageView and UILabel as subviews to it. You would position and style the text and the image but set the UILabel to hidden. Make the whole container view respond to touches. It's easy to do since UIView is derived from UIResponder so all you have to do is override touchesEnded. Whenever you want to change the text label, just set the UILabel to hidden=NO.
There's more to it, however. Notice how the image has rounded corners? You'll want to override the UIImageView's drawRect method to implement a custom drawing routine to do that. There's lots of sample code around and it wasn't part of your original question so I'll stop here.