是否可以在 Storyboard 中旋转图像,还是必须以编程方式进行? [英] Is it possible to rotate an image in Storyboard or do you have to do it programmatically?

查看:29
本文介绍了是否可以在 Storyboard 中旋转图像,还是必须以编程方式进行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个三角形图像要以不同的角度(例如 180 度、90 度)使用.

Assume you have a triangle image you want to use at different angles (e.g., 180 degrees, 90 degrees).

是否可以在 Storyboard 中旋转三角形图像,还是需要以编程方式进行旋转?

Is it possible to rotate the triangle image within Storyboard, or do you need to do it programmatically?

推荐答案

你或许可以创建一个 IBDesignable &IBInspectable UIView 具有旋转角度属性的子类,并将变换应用于它所包含的图像.

You could probably create an IBDesignable & IBInspectable UIView subclass that had a rotation angle property, and applied a transform to the image it contained.

IBInspectable 允许您在 IB 的属性检查器中公开自定义视图的自定义属性.

IBInspectable allows you to expose custom properties of your custom views in IB's Attributes inspector.

制作视图 IBDesignable 允许您在 IB 中查看自定义视图对象的预览.

Making a view IBDesignable allows you to view a preview of your custom view object in IB.

这是一个非常古老的线程,但我决定实现一个允许旋转的自定义 UIView,正如我所描述的.因为现在是 2021 年,所以我使用了 Swift:

This is a very old thread, but I decided to implement a custom UIView that allows rotation, as I described. Since it's now 2021, I used Swift:

@IBDesignable class RotatableView: UIView {

    @objc @IBInspectable var rotationDegrees: Float = 0 {
        didSet {
            print("Setting angle to \(rotationDegrees)")
            let angle = NSNumber(value: rotationDegrees / 180.0 * Float.pi)
            layer.setValue(angle, forKeyPath: "transform.rotation.z")
        }
    }
}

在 Interface Builder 中产生以下结果:

That yields the following in Interface Builder:

这篇关于是否可以在 Storyboard 中旋转图像,还是必须以编程方式进行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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