0

I need to turn the arrow on the map (customAnnotationView) so that the calloutView does not rotate (Example 1).

When I rotate only the image: The arrows are not set in the center and of different sizes (Example 2).

Example 1 enter image description here

Example 2 enter image description here

Example 1
annotationView?.transform = CGAffineTransform(rotationAngle: CGFloat((-180.0 * .pi / 180) - -customPointAnnotation.ang))

Example 2
annotationView?.image = UIImage(named: customPointAnnotation.pinCustomImageName)?.rotate(radians: CGFloat((90.0 * .pi / 180) - -customPointAnnotation.ang))
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
folk-lore
  • 1
  • 3
  • You are not rotating image you are rotating annotation ... so rotate image and then assign it to annotation. – Abu Ul Hassan Aug 01 '19 at 11:22
  • example 2, I rotate the image like that. When I create a subview, they are duplicated after redrawing, as well as the result, as in example 2 – folk-lore Aug 01 '19 at 11:40

1 Answers1

0

I hope it will Solve your problem

 let pinImage = UIImage(named: "customPointAnnotation.pinCustomImageName")
        let size = CGSize(width: 50, height: 50) // size of your choice
        UIGraphicsBeginImageContext(size)
        pinImage!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
        let rotatedImage =  . resizedImage rotate(radians: CGFloat((90.0 * .pi / 180) - -customPointAnnotation.ang)) //if still not set do check by changing image or by slightly changing redians angle values.
        annotationView?.image = rotatedImage
Abu Ul Hassan
  • 1,340
  • 11
  • 28
  • I use your code, it deviates the arrow to the side from the center, as in example 2 . – folk-lore Aug 01 '19 at 12:02
  • slightly change the angle CGFloat((85.0 * .pi / 180). – Abu Ul Hassan Aug 01 '19 at 12:04
  • Or your Arrow image needs to some updations, i am glade it resolved the size issue :) . – Abu Ul Hassan Aug 01 '19 at 12:06
  • CGFloat((85.0 * .pi / 180). – CGFloat ((85.0 * .pi / 180). - so arrows pointing down. the image is even to me, I don’t think that it is a problem – folk-lore Aug 01 '19 at 12:25
  • Okay got the point do not use this method in viewFor annotation all the time because viewFor annotation reuses old annotation and changes its angle according to old annotation angle. – Abu Ul Hassan Aug 01 '19 at 12:41
  • Check an answer over here https://stackoverflow.com/questions/33163155/custom-annotation-image-rotates-only-at-the-beginning-of-the-program-swift-ios – Abu Ul Hassan Aug 01 '19 at 12:43
  • Another good answer is https://stackoverflow.com/questions/30964002/swift-mkannotationview-rotate – Abu Ul Hassan Aug 01 '19 at 12:44
  • i get this error - "Fatal error: Unexpectedly found nil while unwrapping an Optional value" when I redraw annotations – folk-lore Aug 01 '19 at 17:53
  • I can’t turn the arrows correctly. Your method puts them not evenly and spoils the quality of some shooters. If you turn exactly the annotation, then the arrows become smooth and "reuse" does not worsen their display, why? – folk-lore Aug 02 '19 at 08:48
  • https://stackoverflow.com/questions/39508296/how-to-rotate-custom-userlocationannotationview-image-using-mapkit-in-swift check method 3 over here. – Abu Ul Hassan Aug 02 '19 at 09:42