SwiftUI: How to make a transparent Rectangle (.fill(.clear)) receive gestures? - swiftui

I would like to overlay my image with several Rectangle()s, that should respond to gestures (like tapping or dragging). However, I found that when I make the rectangle clear, it stops receiving gestures.
Rectangle()
.fill(.clear)
.gesture(
LongPressGesture()
.onEnded { value in
// this isn't called when the rectangle fill is .clear
}
)
Is there a way to let an invisible element receive taps? I know that I could give it a 1 % opacity, but that feels like an ugly (and visible) kludge.

Add content shape that defines hit-testable area, like
Rectangle()
.fill(.clear)
.contentShape(Rectangle()) // << here !!
.gesture(

One extra way apart from #Asperi's answer is
Color.clear.contentShape(Rectangle())

Related

Swiftui list: trigger an action on tap and mimic basic select then auto-unselect

I'm starting to learn swiftui and I've run into a problem that is both very basic and easily solvable in UIKit; but after spending days searching the internet and watching WWDC videos I've found no native solution.
The premise is simple: I have an array of songs I want to display in a list; when a user taps on a song view it should highlight the view on press, unhighlight after release, and then play the song (ie trigger an action). Sounds simple right?
Here's what I tried and spent way too much time on:
Using List(selection) + .onEvent(changed): I end up with a UUID (because i've only gotten selection to work with a UUID) that I then have to check against an array of songs to match AND the cell won't unhighlight/select itself; even when I try to manually set the State variable to nil or another generated UUID.
Using .onTap (either on or in the cell): I have to tap on the text of the cell to trigger onTap so I get a lot of taps that just don't work (because I have lots of white space in the cell). I also don't get a nice UI color change on press/release.
So after spending hours trying many different things I've finally come up with a solution and I basically wanted to create an account and share it to hopefully help other developers in my position. Because this so very annoyed me that something so basic took so much effort and time to do.
In the end the best solution I came up with was this:
Using ZStack and an empty button:
edit: I found I need to include and hide the content otherwise the button doesn't grow to fill the space (seems in lists it does for some reason). Though not sure what the hit on performance is of rendering the content twice when hiding it. Maybe a GeometryReader would work better?
struct SelectionView: ViewModifier {
let onSelect: () -> Void
func body(content: Content) -> some View {
ZStack (alignment: .leading) {
Button {
onSelect()
} label: {
content
.hidden()
}
content
}
}
}
extension View {
func onSelection(_ selection: #escaping () -> Void) -> some View {
self.modifier(SelectionView(onSelect: selection))
}
}
then to use it:
SongCell(song: song)
.onSelection {
// Do whatever action you want
}
No messing around with list selection, no weird tap hit boxes, and get the press/release color change. Basically put an empty button in a ZStack and trigger off it's action. Could possibly cause tap/touch issues with more complicated cells (?) but it does exactly what I need it to do for my basic app. I'm just not sure why it took so much effort and why apple doesn't support such a basic use case by default? If I've overlooked something native please do inform me. Thanks.
I got the basic idea what you are trying to do. I'm Going to show simple example. Maybe using this you will be able to find proper solution.
First let's create a color : -
#State var colorToShow : Color = Color.blue
Now in body we have our ZStack or Your cell that we want to deal with : -
ZStack{
colorToShow
}.frame(width: 50, height: 50).padding()
.onLongPressGesture(minimumDuration: 3) {
print("Process Complete")
colorToShow = .green
} onPressingChanged: { pressing in
if pressing {
print("Pressing")
colorToShow = .red
} else {
print("Pressing Released")
colorToShow = .blue
}
}
Here we are using .onLongPressGesture. You can set minimum duration on which you want to perform action. Now on process completion You set what you want to do. OnPressingChange give you a bool value that changes according to user is pressing that button or not. Show color change(Highlight) or do action while bool value is true. When user release button do action or unhighlight since bool value turns false.
Hope you find it useful.

Modify ProgressView with drag gestures in SwiftUI

Any idea how to modify (if is possible) a ProgressView with drag gestures. To begin with, I think the code will be something like this:
ProgressView(value: progress)
.gesture(DragGesture()
.onChanged({
// code here
})
But I'm really lost. Any help?
The .onChanged modifier has a parameter in the closure. You can do something like:
.onChanged{ val in
progress = val.translation.width / 100 // perhaps cut it so the number is between 0 and 1
}

SwiftUI border around dynamically shaped View

Sometimes a view's shape can be dynamic like when using the native .buttonStyle(.bordered) modifier. This modifier will apply different corner radius based on different button content sizes:
Question
How can we stroke around a custom view? (not a Shape)
Considerations after trials:
We can't use another button with bigger/smaller frame to create the border illusion, because the corner radius will be different and not matched with the original shape
The view has transparency, so using shadow effect may not be a good option to create the border illusion
Using scale effect not works as desired, because of the different sizes issue, and also the lack of precise border width
ContainerRelativeShape is not working (yet) except for widgets
Not found solutions to try:
Get the shape of a view to apply stroke on it
Get the path around the content of the view
apply some sort of stroke directly on a view
Sample Code:
VStack {
Button {
} label: {
Text("Hello World")
.frame(height: 164) // <- Difference
}
Button {
} label: {
Text("Hello World")
.frame(height: 64) // <- Difference
}
}
.buttonStyle(.bordered)
/* .stroke() // something like this, but for a view */
Unfortunately, borders are drawn around the frame of a view. If you want to add a border to a rounded view, you need to know the corner radius.
You can hardcode the corner radius of your buttons using the .buttonBorderShape() modifier, then overlay your buttons with a shape that matches the shape you choose in .buttonBorderShape()

SwiftUI - ScrollView move content to be visible with KeyboardAwareSwiftUI

I found this answer great for views but for a scrollview it works with this half text view height effect:
Is this something I can do with this KeyboardAwareSwiftUI classes? I tried to play with magical numbers to increase this values here:
func body(content: Content) -> some View {
content
.padding(.bottom, self.keyboard.height + 100)
.edgesIgnoringSafeArea(self.keyboard.height > 0 ? .bottom : [])
.animation(.easeOut)
}
but this just increased some area above the keyboard but text view is still hidden a bit:
I would recommend using this library instead, and you never have to worry about view positioning when keyboard is shown: https://github.com/hackiftekhar/IQKeyboardManager
It's a non swiftui library, however, this issue here shows how to add it to your swiftui app seamlessly and only a few line of code:
https://github.com/hackiftekhar/IQKeyboardManager/issues/1606
hope this helps

Extra Space in List (left and right side) SwiftUI

I am trying to create list screen but while creating a cell it drop a extra space right and left side.
I put only text for reference. I refer some link but it not helps me. and also tried other padding related modifier but not help it also to me.
Code
.......
let text = "Hello stack work is going very fine. let's check the code is woking or not properly"
var body: some View {
List {
Text(text).background(Color.blue)
Text(text).background(Color.blue)
}.foregroundColor(.white)
}
.....
image I attached space indicated with arrow
List drop a extra space right and left side, so you can use padding() modifier to resolve issue.
VStack(alignment: .leading) {
List {
Text("Hello stack work is going very fine. let's check the code is woking or not properly")
.background(Color.blue)
Text("Hello stack work is going very fine. let's check the code is woking or not properly")
.background(Color.blue)
}
.foregroundColor(.white)
}
.padding(.leading, -16) // you can use as per your requirement(-16)
.padding(.trailing, -20)
The List adds a bit of padding on both sides, just like UITableView does. It generally helps readability, but if you insist on removing it, you can add
.padding(-10.0)
to your body.