我真的很想使用 SwiftFortuneWheel 包中的一些功能。这个包是基于 UIKit 构建的,我的实现是 SwiftUI。包上的文档对我来说不够用,因为我没有设法创建一个有效的 UIViewControllerRepresentable 实现。
SwiftFortuneWheel 包:https ://github.com/sh-khashimov/SwiftFortuneWheel
示例项目:https ://github.com/sh-khashimov/SwiftFortuneWheel/tree/master/Examples
我写了以下内容,但在wheelBackgroundView 上不断收到“在展开操作值时发现为零”。我假设我错过了基于 SwiftUI 的项目中不存在的某种 UIKit 实现。有人可以帮助我吗?
struct WheelView: View {
var body: some View {
VStack {
WheelOfFortune()
.frame(width: 400, height: 500)
}
}
}
struct WheelOfFortune: UIViewControllerRepresentable {
typealias UIViewControllerType = VariousWheelPodiumViewController
@State var wheelOfFortune: VariousWheelPodiumViewController = VariousWheelPodiumViewController()
func makeUIViewController(context: Context) -> VariousWheelPodiumViewController {
wheelOfFortune
}
func updateUIViewController(_ uiViewController: VariousWheelPodiumViewController, context: Context) {
//code
}
}
class VariousWheelJackpotViewController: UIViewController {
@IBOutlet weak var wheelBackgroundView: UIView! {
didSet {
wheelBackgroundView.layer.cornerRadius = wheelBackgroundView.bounds.width / 2
}
}
@IBOutlet weak var wheelControl: SwiftFortuneWheel! {
didSet {
wheelControl.configuration = .variousWheelJackpotConfiguration
wheelControl.slices = slices
wheelControl.spinImage = "redCenterImage"
wheelControl.pinImage = "redArrow"
wheelControl.isSpinEnabled = false
wheelControl.pinImageViewCollisionEffect = CollisionEffect(force: 15, angle: 30)
wheelControl.edgeCollisionDetectionOn = true
}
}
var prizes = [(name: "2 T", color: #colorLiteral(red: 0.5854218602, green: 0.8424194455, blue: 0.2066773474, alpha: 1)),
(name: "1 T ", color: #colorLiteral(red: 0.7690342069, green: 0.1236859187, blue: 0.4755392671, alpha: 1)),
(name: "8 G", color: #colorLiteral(red: 0.9826856256, green: 0.7220065594, blue: 0.05612647533, alpha: 1)),
(name: "9 G", color: #colorLiteral(red: 0.2356889248, green: 0.8724163771, blue: 0.85432899, alpha: 1)),
(name: "4 T", color: #colorLiteral(red: 0.9794624448, green: 0.6201235652, blue: 0.6958915591, alpha: 1)),
(name: "7 T", color: #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1))]
lazy var slices: [Slice] = {
var slices: [Slice] = []
for prize in prizes {
let sliceContent = [Slice.ContentType.text(text: prize.name, preferences: .variousWheelJackpotText)]
let slice = Slice(contents: sliceContent, backgroundColor: prize.color)
slices.append(slice)
}
let imagePreferences = ImagePreferences(preferredSize: CGSize(width: 10, height: 60), verticalOffset: 5)
return slices
}()
var finishIndex: Int {
return 0
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//print(wheelBackgroundView)
wheelBackgroundView.layer.cornerRadius = wheelBackgroundView.bounds.width / 2
}
@IBAction func rotateTap(_ sender: Any) {
wheelControl.startRotationAnimation(finishIndex: finishIndex, continuousRotationTime: 1) { (finished) in
print(finished)
}
}
}
public extension SFWConfiguration {
static var variousWheelJackpotConfiguration: SFWConfiguration {
let anchorImage = SFWConfiguration.AnchorImage(imageName: "blueAnchorImage", size: CGSize(width: 12, height: 12), verticalOffset: -22)
let pin = SFWConfiguration.PinImageViewPreferences(size: CGSize(width: 13, height: 40), position: .top, verticalOffset: -25)
let spin = SFWConfiguration.SpinButtonPreferences(size: CGSize(width: 20, height: 20))
let sliceColorType = SFWConfiguration.ColorType.customPatternColors(colors: nil, defaultColor: .white)
let slicePreferences = SFWConfiguration.SlicePreferences(backgroundColorType: sliceColorType, strokeWidth: 0, strokeColor: .white)
let circlePreferences = SFWConfiguration.CirclePreferences(strokeWidth: 15, strokeColor: .black)
var wheelPreferences = SFWConfiguration.WheelPreferences(circlePreferences: circlePreferences, slicePreferences: slicePreferences, startPosition: .top)
wheelPreferences.centerImageAnchor = anchorImage
let configuration = SFWConfiguration(wheelPreferences: wheelPreferences, pinPreferences: pin, spinButtonPreferences: spin)
return configuration
}
}
public extension TextPreferences {
static var variousWheelJackpotText: TextPreferences {
var font = UIFont.systemFont(ofSize: 13, weight: .bold)
var horizontalOffset: CGFloat = 0
if let customFont = UIFont(name: "ToyBox", size: 13) {
font = customFont
horizontalOffset = 2
}
var textPreferences = TextPreferences(textColorType: SFWConfiguration.ColorType.customPatternColors(colors: nil, defaultColor: .white),
font: font,
verticalOffset: 5)
textPreferences.horizontalOffset = horizontalOffset
textPreferences.orientation = .vertical
textPreferences.alignment = .right
return textPreferences
}
}