gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 22:18:09 25 4
gpt4 key购买 nike

如何制作具有角度的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com