How to rotate custom userLocationAnnotationView image using MapKit in swift?
I can't give you complete code examples at this point but I hope I can give you some pointers to go off on.
First, I don't think you would necessarily have to subclass MKAnnotationView
. You could simply assign your UIImage
to its image
property. I think that would make things easier unless you do require the customization.
Now, I assume you have successfully added the annotation to the map and have a reference to it.
To rotate the heading indicator, I see three options:
- Rotate
MKAnnotationView
'simage
property:- Method: When the heading changes, create a rotated copy of the
UIImage
and assign it to theimage
property. Example (not tested). - Con: Can't easily animate the rotation.
- Method: When the heading changes, create a rotated copy of the
- Rotate the
MKAnnotationView
itself:- Method: When the heading changes, use
MKAnnotationView
's/UIView
'stransform
property. Assign an appropriateCGAffineTransform
to it. - Pro: Easiest
- Con: Also rotates the detail/callout views. If you need these, this will not be an option for you.
- Method: When the heading changes, use
- Put the
UIImage
into aUIImageView
and add that one as a subview toMKAnnotationView
:- Method: Similar to 2. but use the
transform
property on theUIImageView
directly not on theMKAnnotationView
itself. This way callout views are not rotated. - Pro: Should work well.
- Con: A bit more work.
- Method: Similar to 2. but use the
What you also need:
- A function to convert from degrees to radians. The affine transformations require radians.
- If you want to animate the rotation (except for 1.) wrap the change to the
transform
property inUIView
's staticanimate
method.
Custom bearing roatation for mapview
Just introduce property and store the new value from the start. Then, when user pushes the button rotate the map from stored value.
var knownHeading: CLHeading
var buttonPushed = false
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
if buttonPushed {
mapView.animate(toBearing: newHeading.magneticHeading)
} else {
knownHeading = newHeading
}
}
@IBAction func buttonPushed() {
mapView.animate(toBearing: knownHeading.magneticHeading)
buttonPushed = true
//your other code here
}
How to rotate image based on angle in Swift?
Try the following code in the delegate method to rotate your image.
let oldRad:Float = 1.259374
let newRad:Float = 1.239832
let theAnimation = CABasicAnimation(keyPath: "transform.rotation")
theAnimation.fromValue = NSNumber(float: oldRad)
theAnimation.toValue = NSNumber(float: newRad)
theAnimation.duration = 0.2
self.compassImage!.layer.addAnimation(theAnimation, forKey: "animateMyRotation")
self.compassImage!.transform = CGAffineTransformMakeRotation(CGFloat(newRad))
Related Topics
How to Catch Accessibility Focus Changed
Uicollectionviewcontroller Error in Swift 3.0: Must Be Initialized with a Non-Nil Layout Parameter
Swift and Nsuserdefaults - Exc_Bad_Instruction When User Defaults Empty
Swift: Nil Is Incompatible with Return Type String
Coretelephony Esim Functions Not Working on Device
Using Simple Ping in Swift (Ios)
How to Crop Image in Circle Shape with Grid Inside Circle
Update Storyboard Constraint Using Code
Generics Type Constraint VS Inheritance
Cgaffinetransformmakerotation with Negative M_Pi
Swiftui Present View Modally via Tabview
Xcode 6 Ignoring Breakpoints - Swift
Get "No Keychain Available" Error When Try to Access Keychain from App Extension
Lineargravityfield() Is Not Affecting Physics Bodies in the Scene Scenekit
Navigationlink Inside Lazyvgrid Cycles All Entries on Back, Swiftui