如何围绕自己的中心旋转子视图? [英] How to rotate sub-views around their own centres?

查看:130
本文介绍了如何围绕自己的中心旋转子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to rotate a view around its own centre?

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

In my present implementation the sub-views (buttons, etc) seem to move along a funny path before they end up in the desired positions (since the super view's coordinate system is rotated). I would like the views to spin 90 degrees around theire own centres as in the picture below.

推荐答案

我真的很喜欢这个问题。我还发现一些接口的旋转动画会震动。这是我如何实现你所描绘的。一个简单的 @interface 就可以了。
注意:我正在使用ARC。

I really like this question. I also find the rotation animation jarring for some interfaces. Here's how I would implement what you have pictured. A simple @interface will be just fine. Note: I am using 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

对<$ c $中的按钮进行适当的插座连接c> .xib :

ControllerWithRotatingButtons.m:

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

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

And that's it. Now when you rotate your device the screen will not rotate but the buttons will as shown:

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

Of course if you only want the button's labels to turn you would simply apply the transform to the _buttonA.titleLabel instead.

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

Note: Please note that once the device is rotated as far as any touches not on the buttons are concerned the device is still in portrait, but your response to my comment seems to indicate that's not a problem for you.

随意离开如果您有相关问题,请发表评论。

Feel free to leave a comment if you have a related question.

这篇关于如何围绕自己的中心旋转子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆