仅旋转视图而不旋转子视图 [英] Rotate only view and not its subviews
问题描述
有什么方法可以将旋转视图的变换传递到其子视图? 我有一个自定义的MKAnnotationView,它正在根据用户的航向值旋转.在自定义注释视图类的以下方法中,我将一个子视图添加到注释视图.
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];
}
但是问题是当我的注释视图旋转时,然后我选择了任何注释,myCallOutView也会出现旋转,这是我不想要的.我正在使用下面的行旋转自定义批注视图
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)];
如何避免这种情况?在将其添加为子视图之前,是否需要对myCallOutView进行一些转换?
How can i avoid this situation? Do i need to apply some transform on myCallOutView before adding it as a subview?
推荐答案
一个非常简单的解决方案是对子视图执行反向旋转:
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.
替代方法::视图的结构略有不同:与其将视图A包含在内部视图B中,还应将视图A和B都作为容器视图C的子视图.然后将旋转应用于视图A,B不会受到影响.并要在整个显示器上移动整体,请重新定位容器视图C.
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.
这篇关于仅旋转视图而不旋转子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!