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

时间:2014-07-09 18:06:24

标签: swift

如何制作具有角度的IBDesignable组件:旋转视图的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)

但它不起作用

1 个答案:

答案 0 :(得分:1)

您应该创建从CustomButton

继承的UIButton
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