Skip to content

Commit

Permalink
tap to slice fixes, tap macOS port
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-khashimov committed Jan 9, 2021
1 parent 48bad3c commit 02c8deb
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Documentation/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## CHANGELOG

### 1.3.3
- Fixed issue when the tap to slice can return incorrect slice index;
- Tap gesture recognizer ported to macOS;

### 1.3.2
- [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);
- Refactoring;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class ViewController: NSViewController {

wheelControl.edgeCollisionDetectionOn = true

wheelControl.wheelTapGestureOn = true
wheelControl.onWheelTap = { index in
print("tapped index: \(index)")
}

return wheelControl
}()

Expand Down
21 changes: 15 additions & 6 deletions Sources/SwiftFortuneWheel/Extensions/CGPoint+Math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,27 @@ extension CGPoint {
/// - comparisonPoint: Circle center point
/// - startPositionOffsetAngle: Circle rotation offset angle
/// - Returns: Angle
func angle(to comparisonPoint: CGPoint, startPositionOffsetAngle: CGFloat) -> CGFloat {
func angle(to comparisonPoint: CGPoint, startPositionOffsetAngle: CGFloat, rotationOffsetAngle: CGFloat) -> CGFloat {

let originX = comparisonPoint.x - x
#if os(macOS)
// antiClockwise = -1
let circleDrawDirection: CGFloat = -1
#else
// Clockwise = 1
let circleDrawDirection: CGFloat = 1
#endif

let originY = comparisonPoint.y - y
let originX = comparisonPoint.x - x
let originY = circleDrawDirection * (comparisonPoint.y - y)

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

let offsetDegreeForTop: CGFloat = 90
let offsetDegreeForTopPosition: CGFloat = 90
let _startPositionOffsetAngle = startPositionOffsetAngle * circleDrawDirection
let _rotationOffsetAngle = rotationOffsetAngle * circleDrawDirection

var bearingDegrees = CGFloat(bearingRadians).degrees - offsetDegreeForTopPosition - _startPositionOffsetAngle + _rotationOffsetAngle

var bearingDegrees = CGFloat(bearingRadians).degrees - offsetDegreeForTop - startPositionOffsetAngle

while bearingDegrees < 0 {
bearingDegrees += 360
}
Expand Down
9 changes: 5 additions & 4 deletions Sources/SwiftFortuneWheel/SwiftFortuneWheel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public class SwiftFortuneWheel: SFWControl {
open var slices: [Slice] = [] {
didSet {
self.wheelView?.slices = slices
self.animator.resetRotationPosition()
}
}

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

guard let frame = wheelView?.frame else { return }
guard let wheelViewBounds = wheelView?.bounds else { return }

guard !animator.isRotating else { return }

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

/// `wheelView` center position
let center = CGPoint(x: frame.midX, y: frame.midY)
let center = CGPoint(x: wheelViewBounds.midX, y: wheelViewBounds.midY)

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

/// `wheelView` rotated position offset angle
let stoppedOffsetPositionAngle = self.animator.currentRotationPosition ?? 0
let rotationOffsetAngle = self.animator.currentRotationPosition ?? 0

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

/// slice index at tap point
let sliceIndex = index(fromAngle: angle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ class SpinningWheelAnimator: NSObject {
self.animationObject?.layerToAnimate?.removeAllAnimations()
stopCollisionDetectorsIfNeeded()
}

func resetRotationPosition() {
currentRotationPosition = nil
}
}

// MARK: - CAAnimationDelegate
Expand Down
4 changes: 2 additions & 2 deletions SwiftFortuneWheel.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "SwiftFortuneWheel"
s.version = "1.3.2"
s.version = "1.3.3"
s.summary = "Ultimate spinning wheel control that supports dynamic content and rich customization."
s.description = <<-DESC
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.
Expand All @@ -20,4 +20,4 @@ Pod::Spec.new do |s|
s.frameworks = "Foundation"
s.ios.framework = 'UIKit'
s.osx.framework = 'AppKit'
end
end

0 comments on commit 02c8deb

Please sign in to comment.