如何旋转他们自己的中心周围的子视图?

是否可以围绕自己的中心旋转视图?

在我现在的实现中,子视图(button等)似乎沿着有趣的path移动,直到它们到达期望的位置(因为超级视图的坐标系被旋转)。 如下图所示,我希望这些意见能围绕自己的中心旋转90度。

期望的旋转

我真的很喜欢这个问题。 我也发现一些接口的旋转animation震撼。 以下是我将如何实现你所描绘的。 一个简单的@interface将会很好。 注意:我正在使用ARC。

 #import <UIKit/UIKit.h> @interface ControllerWithRotatingButtons : UIViewController @property (strong, nonatomic) IBOutlet UIButton *buttonA; @property (strong, nonatomic) IBOutlet UIButton *buttonB; @property (strong, nonatomic) IBOutlet UIButton *buttonC; @end 

.xib的button进行适当的sockets连接:

在这里输入图像说明

ControllerWithRotatingButtons.m:

 #import "ControllerWithRotatingButtons.h" @implementation ControllerWithRotatingButtons @synthesize buttonA = _buttonA; @synthesize buttonB = _buttonB; @synthesize buttonC = _buttonC; -(void)deviceRotated:(NSNotification *)note{ UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; CGFloat rotationAngle = 0; if (orientation == UIDeviceOrientationPortraitUpsideDown) rotationAngle = M_PI; else if (orientation == UIDeviceOrientationLandscapeLeft) rotationAngle = M_PI_2; else if (orientation == UIDeviceOrientationLandscapeRight) rotationAngle = -M_PI_2; [UIView animateWithDuration:0.5 animations:^{ _buttonA.transform = CGAffineTransformMakeRotation(rotationAngle); _buttonB.transform = CGAffineTransformMakeRotation(rotationAngle); _buttonC.transform = CGAffineTransformMakeRotation(rotationAngle); } completion:nil]; } -(void)viewDidLoad{ [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil]; } -(void)viewDidUnload{ [super viewDidUnload]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; } -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end 

就是这样。 现在,当你旋转你的设备时,屏幕将不会旋转,但button将如图所示:

在这里输入图像说明

当然,如果你只想要button的标签,你可以简单地将变换应用到_buttonA.titleLabel

注意:请注意,一旦设备旋转,只要触摸不button,设备仍然是肖像,但你对我的评论的回应似乎表明,这对你来说不是问题。

如果您有相关问题,请随时留下评论。

您可以使用transform属性手动旋转任何UIView子类。

 #import <QuartzCore/QuartzCore.h> myView.transform = CGAffineTransformMakeRotation(M_PI/2);