问题
Is there any way by which transform of a view for rotation wont be passed to its subviews? I have a custom MKAnnotationView which I am rotating according to the heading values of the user. In below method in the custom Annotation view class , i am adding a subview to the annotation view.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if(selected)
{
[self addSubview:self.myCallOutView];
return;
}
[self.myCallOutView removeFromSuperview];
}
but the problem is when my annotation view is rotated and then I select any annotation, myCallOutView also appears rotated which I dont want. I am rotating custom Annotation view using below line
[self setTransform:CGAffineTransformMakeRotation(angle * M_PI / -180.0)];
How can i avoid this situation? Do i need to apply some transform on myCallOutView before adding it as a subview?
回答1:
One very simple solution would be to perform the reverse rotation on the subview:
[self setTransform:CGAffineTransformMakeRotation(angle * M_PI / -180.0)];
// NOTE: we're using the negative of the angle here
[self.myCallOutView setTransform:CGAffineTransformMakeRotation(- angle * M_PI / -180.0)]];
This will cancel out the effect of the rotation on the superview.
Alternative approach: structure your views a little differently: instead of having view A inside view B, have both views A and B being subviews of a container view C. Then just apply your rotation to view A, B won't be affected. And to move the whole ensemble on the display, relocate the container view C.
来源:https://stackoverflow.com/questions/15546510/rotate-only-view-and-not-its-subviews