Hi everybody I am making an attempt to create a Crop View
for cropping pictures chosen by the person by way of using PhotoPicker
The UI appears excellent and the implementations of the varied gestures additionally appear to work as I need however I’ve an issue… The masks situated above the picture respects the protected space, nonetheless I would like its dimensions to be equal to the gadget.
I attempted to enter .ignoreSafeArea()
however this disables the DragGesture
and I can not determine the right way to remedy this downside.. I would like the masks to disregard the gadget’s protected space
nevertheless it should not disable the DragGesture
… it is positively an issue of overlapping views however I can not perceive the place I am fallacious..
struct DraggableImageView: View {
@State non-public var offset = CGSize.zero
@State non-public var lastOffset = CGSize.zero
@State non-public var scale: CGFloat = 1.0 // Scala iniziale dell'immagine (100%)
@State non-public var lastScale: CGFloat = 0
@GestureState non-public var isInteractionActive = false // Stato dell'interazione
let selectedPhoto: UIImage // Immagine selezionata
var physique: some View {
GeometryReader { geometry in
let dimension = geometry.dimension
let imageSize: CGFloat = dimension.width * 0.9 // 90% della larghezza, dimensione del cerchio
ZStack {
// Maschera circolare
Rectangle()
.strokeBorder(Colour.white, lineWidth: 2)
.body(width: imageSize, top: imageSize)
.overlay(
Picture(uiImage: selectedPhoto)
.resizable()
.scaledToFill() // Riempie la maschera circolare
.offset(offset)
.scaleEffect(scale) // Applica il fattore di zoom
.body(width: imageSize, top: imageSize)
)
// Overlay per l'interazione
Colour.black.opacity(isInteractionActive ? 0.3 : 0.7)
.masks {
Rectangle()
.overlay {
Rectangle()
.body(width: imageSize, top: imageSize)
.blendMode(.destinationOut) // Maschera fuori dalla zona circolare
}
}
.ignoresSafeArea()
.gesture(
DragGesture()
.updating($isInteractionActive, physique: { _, out, _ in
out = true
})
.onChanged { worth in
let translation = worth.translation
offset = CGSize(width: lastOffset.width + translation.width, top: lastOffset.top + translation.top)
}
.onEnded { _ in
// Calcolare i limiti per evitare che l'immagine esca dal cerchio
let imageWidth = imageSize * scale
// Limiti di spostamento (non devono superare il cerchio)
let maxOffsetX = ((imageWidth - imageSize) / 2) / scale
let maxOffsetY = ((imageWidth - imageSize) / 2) / scale
// Impedire che l'immagine esca dal cerchio
offset.width = min(max(offset.width, -maxOffsetX), maxOffsetX)
offset.top = min(max(offset.top, -maxOffsetY), maxOffsetY)
lastOffset = offset // Salva l'offset finale
}
)
.gesture(
MagnificationGesture()
.onChanged { worth in
// Limitiamo lo zoom minimo a garantire che l'immagine non sia mai più piccola del cerchio
scale = max(1.0, min(worth, 3.0)) // Limita la scala tra 100% e 300%
}
.onEnded { _ in
// Quando lo zoom finisce, non cambiamo più i limiti
}
)
}
}
.background(.black)
}
}