Skip to content

Commit 02c8deb

Browse files
committed
tap to slice fixes, tap macOS port
1 parent 48bad3c commit 02c8deb

File tree

6 files changed

+35
-12
lines changed

6 files changed

+35
-12
lines changed

Documentation/Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## CHANGELOG
22

3+
### 1.3.3
4+
- Fixed issue when the tap to slice can return incorrect slice index;
5+
- Tap gesture recognizer ported to macOS;
6+
37
### 1.3.2
48
- [Issue #10](https://github.com/sh-khashimov/SwiftFortuneWheel/issues/10) added tap gesture API to detect selected slice index via tap. For more information see [**API Overview**](API_Overview.md);
59
- Refactoring;

Examples/SwiftFortuneWheelDemo-macOS/SwiftFortuneWheelDemo-macOS/SwiftFortuneWheelDemo-macOS/ViewController.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class ViewController: NSViewController {
3333

3434
wheelControl.edgeCollisionDetectionOn = true
3535

36+
wheelControl.wheelTapGestureOn = true
37+
wheelControl.onWheelTap = { index in
38+
print("tapped index: \(index)")
39+
}
40+
3641
return wheelControl
3742
}()
3843

Sources/SwiftFortuneWheel/Extensions/CGPoint+Math.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,27 @@ extension CGPoint {
1919
/// - comparisonPoint: Circle center point
2020
/// - startPositionOffsetAngle: Circle rotation offset angle
2121
/// - Returns: Angle
22-
func angle(to comparisonPoint: CGPoint, startPositionOffsetAngle: CGFloat) -> CGFloat {
22+
func angle(to comparisonPoint: CGPoint, startPositionOffsetAngle: CGFloat, rotationOffsetAngle: CGFloat) -> CGFloat {
2323

24-
let originX = comparisonPoint.x - x
24+
#if os(macOS)
25+
// antiClockwise = -1
26+
let circleDrawDirection: CGFloat = -1
27+
#else
28+
// Clockwise = 1
29+
let circleDrawDirection: CGFloat = 1
30+
#endif
2531

26-
let originY = comparisonPoint.y - y
32+
let originX = comparisonPoint.x - x
33+
let originY = circleDrawDirection * (comparisonPoint.y - y)
2734

2835
let bearingRadians = atan2f(Float(originY), Float(originX))
2936

30-
let offsetDegreeForTop: CGFloat = 90
37+
let offsetDegreeForTopPosition: CGFloat = 90
38+
let _startPositionOffsetAngle = startPositionOffsetAngle * circleDrawDirection
39+
let _rotationOffsetAngle = rotationOffsetAngle * circleDrawDirection
40+
41+
var bearingDegrees = CGFloat(bearingRadians).degrees - offsetDegreeForTopPosition - _startPositionOffsetAngle + _rotationOffsetAngle
3142

32-
var bearingDegrees = CGFloat(bearingRadians).degrees - offsetDegreeForTop - startPositionOffsetAngle
33-
3443
while bearingDegrees < 0 {
3544
bearingDegrees += 360
3645
}

Sources/SwiftFortuneWheel/SwiftFortuneWheel.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public class SwiftFortuneWheel: SFWControl {
9595
open var slices: [Slice] = [] {
9696
didSet {
9797
self.wheelView?.slices = slices
98+
self.animator.resetRotationPosition()
9899
}
99100
}
100101

@@ -307,24 +308,24 @@ public class SwiftFortuneWheel: SFWControl {
307308
/// - Parameter gesture: Tap Gesture
308309
@objc private func wheelTap(_ gesture: UITapGestureRecognizer) {
309310

310-
guard let frame = wheelView?.frame else { return }
311+
guard let wheelViewBounds = wheelView?.bounds else { return }
311312

312313
guard !animator.isRotating else { return }
313314

314315
/// Tap coordinates in `wheelView`
315316
let coordinates = gesture.location(in: self.wheelView)
316317

317318
/// `wheelView` center position
318-
let center = CGPoint(x: frame.midX, y: frame.midY)
319+
let center = CGPoint(x: wheelViewBounds.midX, y: wheelViewBounds.midY)
319320

320321
/// `wheelView` start position offset angle
321322
let startPositionOffsetAngle = self.configuration?.wheelPreferences.startPosition.startAngleOffset ?? 0
322323

323324
/// `wheelView` rotated position offset angle
324-
let stoppedOffsetPositionAngle = self.animator.currentRotationPosition ?? 0
325+
let rotationOffsetAngle = self.animator.currentRotationPosition ?? 0
325326

326327
/// angle between tap point and `wheelView` center position
327-
let angle = coordinates.angle(to: center, startPositionOffsetAngle: startPositionOffsetAngle + stoppedOffsetPositionAngle)
328+
let angle = coordinates.angle(to: center, startPositionOffsetAngle: startPositionOffsetAngle, rotationOffsetAngle: rotationOffsetAngle)
328329

329330
/// slice index at tap point
330331
let sliceIndex = index(fromAngle: angle)

Sources/SwiftFortuneWheel/Utils/Animation/SpinningWheelAnimator.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ class SpinningWheelAnimator: NSObject {
146146
self.animationObject?.layerToAnimate?.removeAllAnimations()
147147
stopCollisionDetectorsIfNeeded()
148148
}
149+
150+
func resetRotationPosition() {
151+
currentRotationPosition = nil
152+
}
149153
}
150154

151155
// MARK: - CAAnimationDelegate

SwiftFortuneWheel.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "SwiftFortuneWheel"
3-
s.version = "1.3.2"
3+
s.version = "1.3.3"
44
s.summary = "Ultimate spinning wheel control that supports dynamic content and rich customization."
55
s.description = <<-DESC
66
Fortune spinning wheel that supports dynamic content and rich customization. Main Features: Dynamic content, support image, and text; Appearance customization; Drawn and animated using CoreGraphics; Dynamic layout.
@@ -20,4 +20,4 @@ Pod::Spec.new do |s|
2020
s.frameworks = "Foundation"
2121
s.ios.framework = 'UIKit'
2222
s.osx.framework = 'AppKit'
23-
end
23+
end

0 commit comments

Comments
 (0)