swift - 是否可以使用 Swift 旋转 IBDesignable UIButton?

标签 swift

如何制作具有角度的 IBDesignable 组件:旋转 View 的 CGFloat 属性

import UIKit

@IBDesignable
class MyB: UIButton {
    @IBInspectable
    var angle: CGFloat = 0 {
        didSet {
            //What to put here?
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        // Initialization code
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

我试过了

self.transform = CGAffineTransformMakeRotation(angle)

但是没用

最佳答案

您应该创建继承自 UIButtonCustomButton

import UIKit

@IBDesignable
class CustomButton: UIButton {

    @IBInspectable var rotation: Double = 0 {
        didSet {
            rotateButton(rotation: rotation)
        }
    }

    func rotateButton(rotation: Double)  {
        self.transform = CGAffineTransform(rotationAngle: CGFloat(.pi/2 + rotation))
    }
}

你会得到如下输出,

enter image description here

关于swift - 是否可以使用 Swift 旋转 IBDesignable UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24660871/

相关文章:

ios - 创建一个整个 iOS 应用程序都可以访问的网络监听器

ios - UITableViewAutomaticDimension 来适应 subview

swift - 如何分层 UIButtons 和 SpriteNodes

ios - 在单个 ViewController 应用程序中,内存保留周期是不可能的吗? ( swift/IOS)

ios - 使用高阶 Swift 函数的潜在内存泄漏

swift - DispatchWorkItem 不通知主线程

json - Swift Codable - 如何编码和解码字符串化的 JSON 值?

json - Alamofire 响应在 JSON 内容与 SwiftyJSON 一起使用之前包含文本

swift - parameter assigned with default(=default)是什么意思?

ios - iOS 中的简单计时器