feat: Cut hole stroke.

This commit is contained in:
Shibo Lyu 2023-08-15 18:49:04 +08:00
parent 8a4b71757c
commit 428b3eb5e8
3 changed files with 46 additions and 11 deletions

View file

@ -154,12 +154,8 @@ public struct CropImageView<Controls: View>: View {
) )
} }
var rectHole: some View { var cutHole: some View {
RectHoleShape(size: targetSize) DefaultCutHoleView(targetSize: targetSize)
.fill(style: FillStyle(eoFill: true))
.foregroundColor(.black.opacity(0.6))
.animation(.default, value: targetSize)
.allowsHitTesting(false)
} }
@MainActor var control: some View { @MainActor var control: some View {
@ -175,7 +171,7 @@ public struct CropImageView<Controls: View>: View {
public var body: some View { public var body: some View {
underlyingImage underlyingImage
.clipped() .clipped()
.overlay(rectHole) .overlay(cutHole)
.overlay(control) .overlay(control)
} }
} }

View file

@ -1,5 +1,5 @@
// //
// RectHoleShape.swift // DefaultCutHoleShape.swift
// //
// //
// Created by Shibo Lyu on 2023/7/21. // Created by Shibo Lyu on 2023/7/21.
@ -7,7 +7,7 @@
import SwiftUI import SwiftUI
struct RectHoleShape: Shape { struct DefaultCutHoleShape: Shape {
var size: CGSize var size: CGSize
var animatableData: AnimatablePair<CGFloat, CGFloat> { var animatableData: AnimatablePair<CGFloat, CGFloat> {
@ -39,10 +39,10 @@ struct RectHoleShape: Shape {
} }
} }
struct RectHoleShape_Previews: PreviewProvider { struct DefaultCutHoleShape_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
VStack { VStack {
RectHoleShape(size: .init(width: 100, height: 100)) DefaultCutHoleShape(size: .init(width: 100, height: 100))
.fill(style: FillStyle(eoFill: true)) .fill(style: FillStyle(eoFill: true))
.foregroundColor(.black.opacity(0.6)) .foregroundColor(.black.opacity(0.6))
} }

View file

@ -0,0 +1,39 @@
//
// SwiftUIView.swift
//
//
// Created by Shibo Lyu on 2023/8/15.
//
import SwiftUI
struct DefaultCutHoleView: View {
var targetSize: CGSize
var showStroke = true
var background: some View {
DefaultCutHoleShape(size: targetSize)
.fill(style: FillStyle(eoFill: true))
.foregroundColor(.black.opacity(0.6))
}
var stroke: some View {
Rectangle()
.strokeBorder(style: .init(lineWidth: 2))
.frame(width: targetSize.width + 4, height: targetSize.height + 4)
.foregroundColor(.white)
}
var body: some View {
background
.allowsHitTesting(false)
.overlay(showStroke ? stroke : nil)
.animation(.default, value: targetSize)
}
}
struct DefaultCutHoleView_Previews: PreviewProvider {
static var previews: some View {
DefaultCutHoleView(targetSize: .init(width: 100, height: 100))
}
}