Swift 幸运轮变色问题

Swift Fortune Wheel changing color issue

我正在制作一个必须添加幸运轮的应用程序,为此我搜索了 pod 并发现这个库非常有用 https://github.com/sh-khashimov/SwiftFortuneWheel. Almost every thing is customisable but now i'm facing one issue with the library. i have a requirement that each slice color will be change once wheel stop, means winning color will be different and losing color will be different after wheel stop. i try to update it color is changing but now wheel is not stoping at correct index. which was stoping before i update color. can any one let me know what i'm doing wrong here. im pasting my code implentation here too. also i raised detail issue in github too https://github.com/sh-khashimov/SwiftFortuneWheel/issues/20

import UIKit
import SwiftFortuneWheel

class VariousWheelPodiumViewController: UIViewController {

var selectedIndex = -1


@IBOutlet weak var wheelControl: SwiftFortuneWheel!

var prizes = [(id:0,name: "MONEYMONEY", color: #colorLiteral(red: 0.9607843137, green: 0.768627451, blue: 0.568627451, alpha: 1),winningColor: #colorLiteral(red: 0.9333333333, green: 0.6078431373, blue: 0.2705882353, alpha: 1),losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1), textColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)),
              (id:1,name: "GRAPHIC", color: #colorLiteral(red: 0.9843137255, green: 0.9098039216, blue: 0.8274509804, alpha: 1),winningColor: #colorLiteral(red: 0.9607843137, green: 0.768627451, blue: 0.568627451, alpha: 1), losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1),textColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)),
              (id:2,name: "HOME", color: #colorLiteral(red: 0.9607843137, green: 0.768627451, blue: 0.568627451, alpha: 1),winningColor: #colorLiteral(red: 0.9333333333, green: 0.6078431373, blue: 0.2705882353, alpha: 1),losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1), textColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)),
              (id:3,name: "IDEA", color: #colorLiteral(red: 0.9843137255, green: 0.9098039216, blue: 0.8274509804, alpha: 1), winningColor: #colorLiteral(red: 0.9333333333, green: 0.6078431373, blue: 0.2705882353, alpha: 1), losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1),textColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)),
              (id:4,name: "MANAGMENT", color: #colorLiteral(red: 0.9607843137, green: 0.768627451, blue: 0.568627451, alpha: 1),winningColor: #colorLiteral(red: 0.9333333333, green: 0.6078431373, blue: 0.2705882353, alpha: 1), losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1),textColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)),
              (id:5,name: "SEARCH", color: #colorLiteral(red: 0.9843137255, green: 0.9098039216, blue: 0.8274509804, alpha: 1),winningColor: #colorLiteral(red: 0.9333333333, green: 0.6078431373, blue: 0.2705882353, alpha: 1),  losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1),textColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)),
              (id:6,name: "TARGET", color: #colorLiteral(red: 0.9607843137, green: 0.768627451, blue: 0.568627451, alpha: 1),winningColor: #colorLiteral(red: 0.9333333333, green: 0.6078431373, blue: 0.2705882353, alpha: 1),  losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1),textColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)),
              (id:7,name: "TIME", color: #colorLiteral(red: 0.9843137255, green: 0.9098039216, blue: 0.8274509804, alpha: 1),winningColor: #colorLiteral(red: 0.9333333333, green: 0.6078431373, blue: 0.2705882353, alpha: 1), losingColor: #colorLiteral(red: 0.737254902, green: 0.2901960784, blue: 0.6156862745, alpha: 1),textColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1))]



var slices = [Slice] ()

var finishIndex: Int {
    return Int.random(in: 0..<wheelControl.slices.count)
}



override func viewDidLoad() {
    super.viewDidLoad()

    wheelControl.configuration = .variousWheelPodiumConfiguration
    wheelControl.spinImage = "center"
    wheelControl.isSpinEnabled = false
    updateSlice()
    wheelControl.slices = slices
    
    
}

@IBAction func rotateTap(_ sender: Any) {
    let finishingIndex = self.finishIndex
    print(finishingIndex)
    wheelControl.startRotationAnimation(finishIndex: finishingIndex, continuousRotationTime: 1) { (isFinished) in
        guard isFinished else { return }
        print(self.prizes[finishingIndex])
        self.selectedIndex = finishingIndex
        self.updateSlice()
        self.wheelControl.slices = self.slices
        self.wheelControl.rotate(toIndex: finishingIndex)
    }
}

func updateSlice(){
    slices = []
    for (index,prize) in prizes.enumerated() {
        print(index,prize.name)
        
        var titleTextPreferences = TextPreferences(textColorType: .customPatternColors(colors: nil, defaultColor: .black), font: .systemFont(ofSize: 10, weight: .bold), verticalOffset: 20)
            titleTextPreferences.horizontalOffset = 10
            titleTextPreferences.orientation = .vertical
            titleTextPreferences.spacing = 0
            titleTextPreferences.alignment = .left
            
        var descriptionTextPreferences = TextPreferences(textColorType: .customPatternColors(colors: nil, defaultColor: .red), font: .systemFont(ofSize: 10), verticalOffset:-75)

        descriptionTextPreferences.horizontalOffset = 0
        descriptionTextPreferences.orientation = .vertical
        descriptionTextPreferences.spacing = 0
        descriptionTextPreferences.alignment = .left

        let content: [Slice.ContentType] = [.text(text: prize.name, preferences: titleTextPreferences),.text(text: prize.name, preferences: descriptionTextPreferences)]

        var slice = Slice(contents: content)
        if selectedIndex == -1{
            slice.backgroundColor = prize.color
        }else{
            slice.backgroundColor = index == selectedIndex ? prize.winningColor : prize.losingColor
        }
        slices.append(slice)
    }
   
}
}

如果您可以遍历 current 切片并直接设置它们的背景颜色,那就太好了。但是,对该代码进行快速测试,它不起作用。显然,只要你触摸一个“切片”对象,轮子就会重置它的旋转。

您可以尝试用这个替换您的 rotateTap(...) 函数:

@IBAction func rotateTap(_ sender: Any) {
    let finishingIndex = self.finishIndex
    print(finishingIndex)
    wheelControl.startRotationAnimation(finishIndex: finishingIndex, continuousRotationTime: 1) { (isFinished) in
        guard isFinished else { return }
        print(self.prizes[finishingIndex])
        self.selectedIndex = finishingIndex
        self.updateSlice()
        self.wheelControl.slices = self.slices
        
        // set rotation async on main queue 
        DispatchQueue.main.async {
            self.wheelControl.rotate(toIndex: finishingIndex)
        }
    }
}

看看这是否会给您带来可接受的结果。如果没有,则有更复杂的解决方法,但最好修改 SwiftFortuneWheel 代码以提供“修改切片 属性” 解决方案。