如何制作具有角度的 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)
但是没用
最佳答案
您应该创建继承自 UIButton
的 CustomButton
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))
}
}
你会得到如下输出,
关于swift - 是否可以使用 Swift 旋转 IBDesignable UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24660871/