How to make an IBDesignable component that has an angle: CGFloat property that rotates the view
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)
}
}
I tried
self.transform = CGAffineTransformMakeRotation(angle)
but it doesn't work
You should create CustomButton
inherited from 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))
}
}
You will get following output,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With