I'm using this code to align the myCompass
-ImageView to the current device heading. (Rotate the image to point in North direction --> it's a compass )
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
let Heading = newHeading.trueHeading.toRadians
UIView.animate(withDuration: 0.5) {
self.myCompass.transform = CGAffineTransform(rotationAngle: CGFloat(-Heading))
}
}
This code is working quiet fine but as you can see I've added a small animation so that the rotation of the IMageView doesn't looks that ugly.
Problem: Now I want to fire an event if the myCompass
-view is pointing exactly to the north pole .
if (Heading > -4 && Heading < 4) {
event()
}
--> Because of the animation I can not check the heading
because it could take about 0.5 seconds
till the needle exactly points to North.
--> I can not check the heading & add a 0.5-delay
because the heading could change within this 0.5 seconds
again
SO to my question:
Is it possible to listen for some kind of specific rotation event of an ImageView?
Or is there some more elegant way to get a result like this?
Maybe something like this pseudo code:
myCompass.onOrientation(rotationAngleMin: 5, rotationAngleMax: -5, event)
func event() {
print("hurray")
}
You can try key-value observing on transform property of you image by:
myCompass.addObserver(self, forKeyPath: "transform", options: [.old, .new], context: nil)
and then observe it in the following method
override func addObserver(_ observer: NSObject, forKeyPath keyPath: String, options: NSKeyValueObservingOptions = [], context: UnsafeMutableRawPointer?) {
if keyPath == "transform" {
// Check your updates here
}
}
Hope it works!!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.