Last active
July 5, 2024 07:02
-
-
Save niw/8538b387c14fc1afc80e2845f2b53930 to your computer and use it in GitHub Desktop.
If you want to implement a view like a capture button on camera app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2024 Yoshimasa Niwa | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
import SwiftUI | |
public struct LongPressAndTapGesture: Gesture { | |
public struct Value: Equatable { | |
public var isLongPressed: Bool | |
public var isTapped: Bool | |
} | |
public var minimumDuration: Double | |
public var maximumDistance: CGFloat | |
public init( | |
minimumDuration: Double = 0.5, | |
maximumDistance: CGFloat = 10 | |
) { | |
self.minimumDuration = minimumDuration | |
self.maximumDistance = maximumDistance | |
} | |
// To track overall touch down and up state change, this implementation uses | |
// DragGesture` with zero `minimumDistance`. | |
// Because custom `Gesture` can not have any state, to know the state change for a recognized | |
// `LongPressGesture` that doesn't change its `Value` when it's recognized at the timing of | |
// `onEnd { ... }` is called, for now, we need to add some sequenced gesture | |
// to change composed gesture value state. | |
// In this implementation, it uses a `LongPressGesture()`, and check composed sequenced gesture | |
// `Value` is `.second(_, .some)` which is the state that the first gesture has been recognized. | |
public var body: AnyGesture<Value> { | |
let dragGesture = DragGesture(minimumDistance: 0.0) | |
let longPressGesture = LongPressGesture( | |
minimumDuration: minimumDuration, | |
maximumDistance: maximumDistance | |
) | |
let tapGesture = TapGesture() | |
return AnyGesture( | |
dragGesture | |
.simultaneously(with: longPressGesture.sequenced(before: LongPressGesture())) | |
.simultaneously(with: tapGesture) | |
.map { value in | |
let sequencedLongPressGestureValue = value.first?.second | |
let isLongPressed: Bool | |
switch sequencedLongPressGestureValue { | |
case .second(_, .some(_)): | |
isLongPressed = true | |
default: | |
isLongPressed = false | |
} | |
let tapGestureValue: Void? = value.second | |
let isTapped = tapGestureValue != nil | |
return Value( | |
isLongPressed: isLongPressed, | |
isTapped: isTapped) | |
} | |
) | |
} | |
} | |
#Preview { | |
struct PreviewView: View { | |
@State | |
private var isFlagged: Bool = false | |
@State | |
private var isPressing: Bool = false | |
@State | |
private var isLongPressing: Bool = false | |
var body: some View { | |
Circle() | |
.foregroundStyle(isFlagged ? .blue : .green) | |
.overlay { | |
Circle() | |
.stroke(.red, lineWidth: isPressing ? (isLongPressing ? 20.0 : 5.0) : 0.0) | |
} | |
.padding() | |
.gesture( | |
LongPressAndTapGesture() | |
.onChanged { value in | |
isPressing = true | |
if value.isLongPressed { | |
isLongPressing = true | |
} | |
} | |
.onEnded{ value in | |
isPressing = false | |
isLongPressing = false | |
if value.isTapped { | |
isFlagged.toggle() | |
} | |
} | |
) | |
} | |
} | |
return PreviewView() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment