Skip to content

Commit

Permalink
Add onSpinEnd action
Browse files Browse the repository at this point in the history
  • Loading branch information
sameersyd committed Apr 7, 2021
1 parent bb39ee1 commit dae451a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
9 changes: 9 additions & 0 deletions Example/FortuneWheel/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@
//

import UIKit
import SwiftUI
import FortuneWheel

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

if #available(iOS 14.0, *) {
let hostingController = UIHostingController(rootView: FortuneWheel(titles: ["Player 1", "Player 2"], size: 320, onSpinEnd: { i in
print("value =", i)
}))
view.addSubview(hostingController.view)
}
}

override func didReceiveMemoryWarning() {
Expand Down
12 changes: 7 additions & 5 deletions Source/FortuneWheel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ import SwiftUI
@available(iOS 14.0, *)
public struct FortuneWheel: View {

var titles: [String], size: CGFloat, strokeWidth: CGFloat, strokeColor: Color = Color(hex: "252D4F")
var titles: [String], size: CGFloat, onSpinEnd: ((Int) -> ())?, strokeWidth: CGFloat, strokeColor: Color = Color(hex: "252D4F")
var colors: [Color] = Color.spin_wheel_color
@StateObject var viewModel: FortuneWheelViewModel

public init(titles: [String], size: CGFloat, colors: [Color]? = nil,
strokeWidth: CGFloat = 15, strokeColor: Color? = nil,
animDuration: Double = Double(6), timeCurveAnimation: Animation? = nil) {
public init(titles: [String], size: CGFloat, onSpinEnd: ((Int) -> ())?,
colors: [Color]? = nil, strokeWidth: CGFloat = 15, strokeColor: Color? = nil,
animDuration: Double = Double(6), timeCurveAnimation: Animation? = nil) {

let animation = Animation.timingCurve(0.51, 0.97, 0.56, 0.99, duration: animDuration)
self.titles = titles
self.size = size
if let colors = colors { self.colors = colors }
self.strokeWidth = strokeWidth
if let strokeColor = strokeColor { self.strokeColor = strokeColor }
_viewModel = StateObject(wrappedValue: FortuneWheelViewModel(titles: titles, animDuration: animDuration,
timeCurveAnimation: timeCurveAnimation ?? animation))
timeCurveAnimation: timeCurveAnimation ?? animation,
onSpinEnd: onSpinEnd))
}

public var body: some View {
Expand Down
7 changes: 5 additions & 2 deletions Source/FortuneWheelViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ class FortuneWheelViewModel: ObservableObject {
private var timeCurveAnimation: Animation
private var pendingRequestWorkItem: DispatchWorkItem?

init(titles: [String], animDuration: Double, timeCurveAnimation: Animation) {
private var onSpinEnd: ((Int) -> ())?

init(titles: [String], animDuration: Double, timeCurveAnimation: Animation, onSpinEnd: ((Int) -> ())?) {
self.titles = titles
self.animDuration = animDuration
self.timeCurveAnimation = timeCurveAnimation
self.onSpinEnd = onSpinEnd
}

func spinWheel() {
Expand All @@ -32,7 +35,7 @@ class FortuneWheelViewModel: ObservableObject {
if let count = self?.titles.count,
let distance = self?.degree.truncatingRemainder(dividingBy: 360) {
let pointer = floor(distance/(360/Double(count)))
print(count - Int(pointer) - 1)
if let onSpinEnd = self?.onSpinEnd { onSpinEnd(count - Int(pointer) - 1) }
}
}
// Save the new work item and execute it after duration
Expand Down

0 comments on commit dae451a

Please sign in to comment.