Skip to content

Commit

Permalink
Merge pull request #11 from benedom/feature/add-zoom-sensitivity-to-c…
Browse files Browse the repository at this point in the history
…onfiguration

Added zoom sensitivity to configuration
  • Loading branch information
benedom authored May 11, 2024
2 parents c959eb9 + ee15f67 commit 4d9d6a2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
14 changes: 13 additions & 1 deletion Demo/SwiftyCropDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct ContentView: View {
@State private var rotateImage: Bool
@State private var maxMagnificationScale: CGFloat
@State private var maskRadius: CGFloat
@State private var zoomSensitivity: CGFloat
@FocusState private var textFieldFocused: Bool

init() {
Expand All @@ -17,6 +18,7 @@ struct ContentView: View {
_rotateImage = State(initialValue: defaultConfiguration.rotateImage)
_maxMagnificationScale = State(initialValue: defaultConfiguration.maxMagnificationScale)
_maskRadius = State(initialValue: defaultConfiguration.maskRadius)
_zoomSensitivity = State(initialValue: defaultConfiguration.zoomSensitivity)
}

var body: some View {
Expand Down Expand Up @@ -91,6 +93,15 @@ struct ContentView: View {
DecimalTextField(value: $maskRadius)
.focused($textFieldFocused)
}

HStack {
Text("Zoom sensitivity")

.frame(maxWidth: .infinity, alignment: .leading)

DecimalTextField(value: $zoomSensitivity)
.focused($textFieldFocused)
}
}
}
.toolbar {
Expand All @@ -117,7 +128,8 @@ struct ContentView: View {
maxMagnificationScale: maxMagnificationScale,
maskRadius: maskRadius,
cropImageCircular: cropImageCircular,
rotateImage: rotateImage
rotateImage: rotateImage,
zoomSensitivity: zoomSensitivity
)
) { croppedImage in
// Do something with the returned, cropped image
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,16 @@ You can also configure `SwiftyCropView` by passing a `SwiftyCropConfiguration`.
| `maskRadius` | `CGFloat`: The radius of the mask used for cropping. Defaults to `130`. A good way is to make it dependend on the screens size. |
| `cropImageCircular` | `Bool`: When using the cropping mask `circle`, whether the resulting image should also be masked as circle. Defaults to `false`. |
| `rotateImage` | `Bool`: Whether the image can be rotated when cropping using pinch gestures. Defaults to `true`. |
| `zoomSensitivity` | `CGFloat`: Zoom sensitivity when cropping. Increase to make zoom faster / less sensitive. Defaults to `1.0`. |

Create a configuration like this:
```swift
let configuration = SwiftyCropConfiguration(
maxMagnificationScale = 4.0,
maskRadius: 130,
cropImageCircular: false,
rotateImage: true
rotateImage: true,
zoomSensitivity = 1.0
)
```
and use it like this:
Expand Down
6 changes: 5 additions & 1 deletion Sources/SwiftyCrop/Models/SwiftyCropConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public struct SwiftyCropConfiguration {
public let maskRadius: CGFloat
public let cropImageCircular: Bool
public let rotateImage: Bool
public let zoomSensitivity: CGFloat

/// Creates a new instance of `SwiftyCropConfiguration`.
///
Expand All @@ -18,15 +19,18 @@ public struct SwiftyCropConfiguration {
/// Defaults to `false`.
/// - rotateImage: Option to rotate image.
/// Defaults to `true`.
/// - zoomSensitivity: Sensitivity when zooming. Default is `1.0`. Decrease to increase sensitivity.
public init(
maxMagnificationScale: CGFloat = 4.0,
maskRadius: CGFloat = 130,
cropImageCircular: Bool = false,
rotateImage: Bool = true
rotateImage: Bool = true,
zoomSensitivity: CGFloat = 1
) {
self.maxMagnificationScale = maxMagnificationScale
self.maskRadius = maskRadius
self.cropImageCircular = cropImageCircular
self.rotateImage = rotateImage
self.zoomSensitivity = zoomSensitivity
}
}
2 changes: 1 addition & 1 deletion Sources/SwiftyCrop/View/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct CropView: View {
var body: some View {
let magnificationGesture = MagnificationGesture()
.onChanged { value in
let sensitivity: CGFloat = 0.2
let sensitivity: CGFloat = 0.1 * configuration.zoomSensitivity
let scaledValue = (value.magnitude - 1) * sensitivity + 1

let maxScaleValues = viewModel.calculateMagnificationGestureMaxValues()
Expand Down

0 comments on commit 4d9d6a2

Please sign in to comment.