VSpinGame
示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
。
需求
- iOS 10.0+
- Xcode 11+
- Swift 5.0+
安装
VSpinGame 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'VSpinGame'
初始化和使用
集成
将 VSpinGame 导入 ViewController.swift
然后在添加的 UIView 上点击,进入 Identity Inspector,将类设置为 VSpinGame
在内部初始化 VSpinGame
class ViewController: UIViewController {
@IBOutlet weak var wheelGame: VSpinGame! {
didSet {
wheelGame.delegate = self
//wheelGame.configuration = .vWheelspinconfiguration
wheelGame.slices = slices
//wheelGame.pinImage = "pin"
//wheelGame.pinImageViewCollisionEffect = CollisionEffect(force: 8, angle: 20)
//wheelGame.edgeCollisionDetectionOn = true
}
}
//Here VDict is a model of response
let p1 = VDict(id: "1", displayText: "$50", value: "50", currency: "USD")
let p2 = VDict(id: "2", displayText: "$100", value: "100", currency: "USD")
let p3 = VDict(id: "3", displayText: "$50", value: "50", currency: "USD")
let p4 = VDict(id: "4", displayText: "$100", value: "100", currency: "USD")
let p5 = VDict(id: "5", displayText: "$50", value: "50", currency: "USD")
let p6 = VDict(id: "6", displayText: "$100", value: "100", currency: "USD")
let p7 = VDict(id: "7", displayText: "$50", value: "50", currency: "USD")
let p8 = VDict(id: "8", displayText: "$100", value: "100", currency: "USD")
var objarray : Array<VDict> = []
lazy var slices: [Slice] = {
objarray = [p1,p2,p3,p4,p5,p6,p7,p8]
let slices = objarray.map({ Slice.init(contents: [Slice.ContentType.text(text: $0.displayText, preferences: .WheelText)], objArray: objarray) })
return slices
}()
var finishIndex: Int {
return Int.random(in: 0..<wheelGame.slices.count)
}
}
配置
您可以将 配置
实例注入到 VSpinGame 中,允许您配置文本、颜色、字体和切片背景色功能
//MARK:- spinConfiguration
public extension VConfiguration {
static var vWheelspinconfiguration: VConfiguration {
let pin = VConfiguration.PinImageViewPreferences(size: CGSize(width: 30,height: 50), position: .top, verticalOffset: -20)
let sliceBackgroundColorType = VConfiguration.ColorType.evenOddColors(evenColor: #colorLiteral(red: 0.07843137255, green: 0.1019607843, blue: 0.1176470588, alpha: 1), oddColor: #colorLiteral(red: 0.01568627451, green: 0.05098039216, blue: 0.07843137255, alpha: 1))
let slicePreferences = VConfiguration.SlicePreferences(backgroundColorType: sliceBackgroundColorType, strokeWidth: 0, strokeColor: #colorLiteral(red: 0.07843137255, green: 0.1019607843, blue: 0.1176470588, alpha: 1))
let circlePreferences = VConfiguration.CirclePreferences(strokeWidth: 14, strokeColor: #colorLiteral(red: 0.07843137255, green: 0.1019607843, blue: 0.1176470588, alpha: 1))
let wheelPreferences = VConfiguration.WheelPreferences(circlePreferences: circlePreferences, slicePreferences: slicePreferences, startPosition: .top)
let configuration = VConfiguration(wheelPreferences: wheelPreferences, pinPreferences: pin)
return configuration
}
}
//MARK:- spinText
public extension TextPreferences {
static var WheelText: TextPreferences {
var textPreferences = TextPreferences(textColorType: VConfiguration.ColorType.customPatternColors(colors: nil, defaultColor: .white),font: .systemFont(ofSize: 16, weight: .bold),verticalOffset: 12)
textPreferences.orientation = .vertical
textPreferences.horizontalOffset = 0
textPreferences.alignment = .right
return textPreferences
}
}
现在让我们将这些连接到 outlets,并连接到名为 wheelGame 的 outlets。
我们希望 View Controller 实现 VSpinGameDelegate 协议,此时 ViewController.swift 应该看起来如下
//MARK:- VSpinGameDelegate
extension ViewController : VSpinGameDelegate{
func onOpen(_ VSpinGame: VSpinGame) {
//print("open")
}
func onError(_ VSpinGame: VSpinGame, requiredArgumentError error: String) {
print(error)
}
func onClose(_ VSpinGame: VSpinGame) {
//print("close")
}
//onReward
func onReward(_ VSpinGame: VSpinGame, didGetRewardPoints result: Int) {
let rewardvalue = VSpinGame.slices[0].objArray[result] as! VDict
print("Reward :", rewardvalue.value)
}
}
当轮盘开始旋转时,它将执行 onStart
方法,其参数为 VSpinGame 的实例,如下所示
@IBAction func onStartTap(_ sender: UIButton) {
wheelGame.onStart(finishIndex: finishIndex, continuousRotationTime: 1) { (finished) in
//print(self.finishIndex)
}
}
作者
vandanapansuria, [email protected]
许可证
VSpinGame 可在 MIT 许可证下使用。更多信息请参阅 LICENSE 文件。