I have an ima开发者_如何转开发ge which shows which way is north on a map. This image is updated by CoreLocation if a new heading is found. It is also placed at the correct position, I use the following code:
locateMeView.transform = CGAffineTransformMakeRotation(([CoreLocationController sharedInstance].heading - 45) * M_PI / 180);
CGRect frame = locateMeView.frame;
CGPoint pos = [MapClass vectorForLocation:[CoreLocationController sharedInstance].location mapCenter:curLocation zoom:curZoom];
frame.origin.x = (int)(self.view.bounds.size.width/2 + pos.x - frame.size.width / 2);
frame.origin.y = (int)(self.view.bounds.size.height/2 + pos.y - frame.size.height / 2);
frame.size.width = 24;
frame.size.height = 24;
locateMeView.frame = frame;
CoreLocationController is a class which stores the updates of the normal CoreLocation. The MapClass transforms lat/lng coordinates to x/y coordinates on my map. The position of the image is correct, but the rotation is causing strange effects. For 0 en M_PI the image is correct but between these the image is stretched as if it is also rotated around the z-axis and at M_PI/2 and 3 * M_PI/2, it disappears altogether. Can someone explain what is happening and what I am doing wrong?
I found out what was wrong (more or less anyway). When using the transform property you are not allowed (for whatever reason) to change the position by changing the frame, you have to use the center property. So the code in the end was:
CGPoint pos = [MapClass vectorForLocation:[CoreLocationController sharedInstance].location mapCenter:curLocation zoom:curZoom];
locateMeView.transform = CGAffineTransformMakeRotation(([CoreLocationController sharedInstance].heading - 45) * M_PI / 180);
locateMeView.center = CGPointMake(int)(self.view.bounds.size.width/2 + pos.x), (int)(self.view.bounds.size.height/2 + pos.y));
Hope that someone with the same problem will find my answer.
Another solution:
- Set transform to identity
- Change the frame
- Set transform
In your situation it should be:
locateMeView.transform = CGAffineTransformIdentity;
CGRect frame = locateMeView.frame;
CGPoint pos = [MapClass vectorForLocation:[CoreLocationController sharedInstance].location mapCenter:curLocation zoom:curZoom];
frame.origin.x = (int)(self.view.bounds.size.width/2 + pos.x - frame.size.width / 2);
frame.origin.y = (int)(self.view.bounds.size.height/2 + pos.y - frame.size.height / 2);
frame.size.width = 24;
frame.size.height = 24;
locateMeView.frame = frame;
locateMeView.transform = CGAffineTransformMakeRotation(([CoreLocationController sharedInstance].heading - 45) * M_PI / 180);
Try setting the transform after the resizing? Order can matter with transforms.
精彩评论