如何围绕自己的中心旋转子视图?

rag*_*ius 36 rotation autorotate ios

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

在我目前的实现中,子视图(按钮等)似乎在它们最终位于所需位置之前沿着有趣的路径移动(因为超视图的坐标系被旋转).我希望这些视图在自己的中心周围旋转90度,如下图所示.

期望的轮换

NJo*_*nes 16

我真的很喜欢这个问题.我还发现一些接口的旋转动画会震动.这是我如何实现你所描绘的.一个简单的@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
Run Code Online (Sandbox Code Playgroud)

适当的插座连接到以下按钮.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
Run Code Online (Sandbox Code Playgroud)

就是这样.现在,当您旋转设备时,屏幕将不会旋转,但按钮将如下所示:

在此输入图像描述

当然,如果您只想转动按钮的标签,则只需将转换应用于转换_buttonA.titleLabel.

注意:请注意,一旦设备旋转到任何不在按钮上的触摸,设备仍处于纵向状态,但您对我的评论的回复似乎表明这对您来说不是问题.

如果您有相关问题,请随时发表评论.