是否可以围绕视图的中心旋转?
在我当前的实现中,子视图(按钮等)似乎会沿着有趣的路径移动,然后才能到达所需的位置(因为超级视图的坐标系被旋转了)。我希望视图像下面图片一样围绕自己的中心旋转90度。
是否可以围绕视图的中心旋转?
在我当前的实现中,子视图(按钮等)似乎会沿着有趣的路径移动,然后才能到达所需的位置(因为超级视图的坐标系被旋转了)。我希望视图像下面图片一样围绕自己的中心旋转90度。
我真的很喜欢这个问题。对于某些界面,我也发现旋转动画很刺眼。这是我如何实现你所描绘的内容。一个简单的@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
文件中,需要将适当的输出连接到按钮上:
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
就是这样。现在当你旋转设备时,屏幕不会旋转,但按钮会如所示:
当然,如果你只想让按钮的标签旋转,那么你只需要将变换应用于_buttonA.titleLabel
即可。
注意:请注意,一旦设备旋转到触摸不在按钮上的程度,设备仍处于竖屏状态,但您对我的评论的回复似乎表明这对您不是问题。
如果您有相关问题,请随时留言。
您可以使用变换属性手动旋转任何UIView子类。
#import <QuartzCore/QuartzCore.h>
myView.transform = CGAffineTransformMakeRotation(M_PI/2);
UIViewController
的旋转回调,比如– shouldAutorotateToInterfaceOrientation:
和– willRotateToInterfaceOrientation:duration:
。看看你能否阻止视图旋转,但仍然订阅旋转事件并相应地更改你的子视图。 - sudo rm -rf