问题
Is it possible to use the native compass that iOS has within my own application? Or do I need to draw and animate my own compass?
回答1:
There is no native compass UIView
. In order to use the magnetometer, you'll have to use CoreLocation and the following delegate method:
- (void) locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading
to rotate a UIView to point North (bearingView is a UIImageView):
float heading = newHeading.magneticHeading; //in degrees
float headingDegrees = (heading*M_PI/180); //assuming needle points to top of iphone. convert to radians
self.bearingView.transform = CGAffineTransformMakeRotation(headingDegrees);
来源:https://stackoverflow.com/questions/9264838/use-native-ios-compass-within-an-app