-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LongPress to focus on LocalVideo (#221)
- Loading branch information
1 parent
219343a
commit 1d8a152
Showing
12 changed files
with
380 additions
and
79 deletions.
There are no files selected for viewing
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
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
83 changes: 83 additions & 0 deletions
83
DemoApp/Sources/Views/LongPressToFocusViewModifier/LongPressToFocusViewModifier.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// Copyright © 2023 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import StreamVideo | ||
import SwiftUI | ||
import AVFoundation | ||
|
||
/// A `ViewModifier` that adds a long press to focus gesture to a SwiftUI view. | ||
struct LongPressToFocusViewModifier: ViewModifier { | ||
|
||
/// The frame within which the focus gesture can be recognized. | ||
var availableFrame: CGRect | ||
|
||
/// The handler to call with the focus point when the gesture is recognized. | ||
var handler: (CGPoint) -> Void | ||
|
||
/// Modifies the content by adding a long press gesture recognizer. | ||
/// | ||
/// - Parameter content: The content to be modified. | ||
/// - Returns: The modified content with the long press to focus gesture added. | ||
func body(content: Content) -> some View { | ||
content | ||
.gesture( | ||
// A long press gesture requiring a minimum of 0.5 seconds to be recognized. | ||
LongPressGesture(minimumDuration: 0.5) | ||
// Sequence the long press gesture with a drag gesture in the local coordinate space. | ||
.sequenced(before: DragGesture(minimumDistance: 0, coordinateSpace: .local)) | ||
.onEnded { value in | ||
// Handle the end of the gesture sequence. | ||
switch value { | ||
// If the long press gesture was succeeded by a drag gesture. | ||
case .second(true, let drag): | ||
// If the drag gesture has a valid location. | ||
if let location = drag?.location { | ||
// Convert the point to the focus interest point and call the handler. | ||
handler(convertToPointOfInterest(location)) | ||
} | ||
// All other cases do nothing. | ||
default: | ||
break | ||
} | ||
} | ||
) | ||
} | ||
|
||
/// Converts a point within the view's coordinate space to a point of interest for camera focus. | ||
/// | ||
/// The conversion is based on the `availableFrame` property, flipping the axis | ||
/// and normalizing the point to the range [0, 1]. | ||
/// | ||
/// - Parameter point: The point to convert. | ||
/// - Returns: The converted point of interest. | ||
func convertToPointOfInterest(_ point: CGPoint) -> CGPoint { | ||
CGPoint( | ||
x: point.y / availableFrame.height, | ||
y: 1.0 - point.x / availableFrame.width | ||
) | ||
} | ||
} | ||
|
||
extension View { | ||
|
||
/// Adds a long press to focus gesture to the view. | ||
/// | ||
/// - Parameters: | ||
/// - availableFrame: The frame within which the focus gesture can be recognized. | ||
/// - handler: The closure to call with the focus point when the gesture is recognized. | ||
/// - Returns: A modified view with the long press to focus gesture added. | ||
@ViewBuilder | ||
func longPressToFocus( | ||
availableFrame: CGRect, | ||
handler: @escaping (CGPoint) -> Void | ||
) -> some View { | ||
// Apply the view modifier to add the gesture to the view. | ||
modifier( | ||
LongPressToFocusViewModifier( | ||
availableFrame: availableFrame, | ||
handler: handler | ||
) | ||
) | ||
} | ||
} |
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
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
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
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
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
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
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
Oops, something went wrong.