如何制作具有角度的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)
但它不起作用
答案 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))
}
}
您将获得以下输出