xcode 如何在 Interface Builder 中旋转 UIImageView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14654096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:41:51  来源:igfitidea点击:

How can I rotate an UIImageView in Interface Builder?

iphonexcodeuiimageviewrotationinterface-builder

提问by gpichler

Is it possible to rotate an UIImageView in Interface Builder from XCode? Like I can do it in Photoshop, where I can scale and rotate an image.

是否可以从 XCode 旋转 Interface Builder 中的 UIImageView?就像我可以在 Photoshop 中那样,我可以在其中缩放和旋转图像。

I know it is possible by code but is there a way to do this in the interface builder?

我知道通过代码是可能的,但是有没有办法在界面构建器中做到这一点?

回答by Leszek Szary

Yes, you can do this in interface builder without any additional code with layer.transform.rotation.zruntime attribute. Note that this value is in radians.

是的,您可以在界面构建器中执行此操作,而无需任何带有layer.transform.rotation.z运行时属性的附加代码。请注意,此值以弧度为单位。

rotation

回转

回答by Gabriele

Yes you can but using a little trick.

是的,你可以,但使用一个小技巧。

Declare in your code a new UIView Category like this

在你的代码中声明一个像这样的新 UIView 类别

@interface UIView (IBRotateView)

@property (nonatomic, assign) CGFloat rotation;

@end


@implementation UIView (IBRotateView)
@dynamic rotation;

- (void)setRotation:(CGFloat)deg
{
    CGFloat rad = M_PI * deg / 180.0;

    CGAffineTransform rot = CGAffineTransformMakeRotation(rad);

    [self setTransform:rot];
}

@end

Now you can use a runtime parameter "rotation" directly on interface builder like this on any UIView you like.

现在,您可以像这样在您喜欢的任何 UIView 上直接在界面构建器上使用运行时参数“旋转”。

enter image description here

在此处输入图片说明

Obviously interface builder will not rotate the view in its internal render because it will only effect the runtime rendering.

显然,界面构建器不会在其内部渲染中旋转视图,因为它只会影响运行时渲染。

回答by Cocoa Dev

You can't do this in the current version of Xcode. You may submit a feature request to Apple at http://radar.apple.com

您不能在当前版本的 Xcode 中执行此操作。您可以在http://radar.apple.com 上向 Apple 提交功能请求

However if you want to do it, you will need to write some code

但是,如果您想这样做,则需要编写一些代码

#define RADIANS(degrees) ((degrees * M_PI) / 180.0)

theView.transform = CGAffineTransformRotate(theView.transform, radians)

回答by Dhruv Goel

Nope thats not possible in the interface builder but the code is quite simple:

不,这在界面构建器中是不可能的,但代码非常简单:

#define RADIANS(degrees) ((degrees * M_PI) / 180.0)

CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity,
     RADIANS(120.0));

imageView.transform = rotateTransform;