diff options
author | leoz <leoz@yahoo.com> | 2024-02-21 11:23:55 -0500 |
---|---|---|
committer | leoz <leoz@yahoo.com> | 2024-02-21 11:23:55 -0500 |
commit | a505b7725bed2d45f9e9566bfe3f719b60d7171c (patch) | |
tree | cda34b5331b572c56c66d4b38e4773b630e5bdfb | |
parent | 2947fb73c8aec82a7e1d589842d69439a7c76164 (diff) |
Refactor gestures
-rw-r--r-- | Sources/SwiftyCrop/View/CropView.swift | 75 |
1 files changed, 38 insertions, 37 deletions
diff --git a/Sources/SwiftyCrop/View/CropView.swift b/Sources/SwiftyCrop/View/CropView.swift index a5abde2..73caaf1 100644 --- a/Sources/SwiftyCrop/View/CropView.swift +++ b/Sources/SwiftyCrop/View/CropView.swift @@ -30,6 +30,43 @@ struct CropView: View { } var body: some View { + let magnificationGesture = MagnificationGesture() + .onChanged { value in + let sensitivity: CGFloat = 0.2 + let scaledValue = (value.magnitude - 1) * sensitivity + 1 + + let maxScaleValues = viewModel.calculateMagnificationGestureMaxValues() + viewModel.scale = min(max(scaledValue * viewModel.scale, maxScaleValues.0), maxScaleValues.1) + + let maxOffsetPoint = viewModel.calculateDragGestureMax() + let newX = min(max(viewModel.lastOffset.width, -maxOffsetPoint.x), maxOffsetPoint.x) + let newY = min(max(viewModel.lastOffset.height, -maxOffsetPoint.y), maxOffsetPoint.y) + viewModel.offset = CGSize(width: newX, height: newY) + } + .onEnded { _ in + viewModel.lastScale = viewModel.scale + viewModel.lastOffset = viewModel.offset + } + + let dragGesture = DragGesture() + .onChanged { value in + let maxOffsetPoint = viewModel.calculateDragGestureMax() + let newX = min( + max(value.translation.width + viewModel.lastOffset.width, -maxOffsetPoint.x), + maxOffsetPoint.x + ) + let newY = min( + max(value.translation.height + viewModel.lastOffset.height, -maxOffsetPoint.y), + maxOffsetPoint.y + ) + viewModel.offset = CGSize(width: newX, height: newY) + } + .onEnded { _ in + viewModel.lastOffset = viewModel.offset + } + + let combinedGesture = magnificationGesture.simultaneously(with: dragGesture) + VStack { Text("interaction_instructions", tableName: localizableTableName, bundle: .module) .font(.system(size: 16, weight: .regular)) @@ -64,43 +101,7 @@ struct CropView: View { ) } .frame(maxWidth: .infinity, maxHeight: .infinity) - .gesture( - MagnificationGesture() - .onChanged { value in - let sensitivity: CGFloat = 0.2 - let scaledValue = (value.magnitude - 1) * sensitivity + 1 - - let maxScaleValues = viewModel.calculateMagnificationGestureMaxValues() - viewModel.scale = min(max(scaledValue * viewModel.scale, maxScaleValues.0), maxScaleValues.1) - - let maxOffsetPoint = viewModel.calculateDragGestureMax() - let newX = min(max(viewModel.lastOffset.width, -maxOffsetPoint.x), maxOffsetPoint.x) - let newY = min(max(viewModel.lastOffset.height, -maxOffsetPoint.y), maxOffsetPoint.y) - viewModel.offset = CGSize(width: newX, height: newY) - } - .onEnded { _ in - viewModel.lastScale = viewModel.scale - viewModel.lastOffset = viewModel.offset - } - .simultaneously( - with: DragGesture() - .onChanged { value in - let maxOffsetPoint = viewModel.calculateDragGestureMax() - let newX = min( - max(value.translation.width + viewModel.lastOffset.width, -maxOffsetPoint.x), - maxOffsetPoint.x - ) - let newY = min( - max(value.translation.height + viewModel.lastOffset.height, -maxOffsetPoint.y), - maxOffsetPoint.y - ) - viewModel.offset = CGSize(width: newX, height: newY) - } - .onEnded { _ in - viewModel.lastOffset = viewModel.offset - } - ) - ) + .gesture(combinedGesture) HStack { Button { |