I can't stress enough how important and helpful your content is! Thanks a lot for making these! I was kinda lost and couldn't structure my learning on SwiftUI, but your videos are just unbelievably well-structured... I want to say this again, thank you for your work and enthusiasm. Swift is actually fun, you make it fun, Nick! An awesome teaching.
I am creating a chess game and I wanted to be able to drag the pieces around to make moves. Spent lots of time trying to wrap my head around .onDrag and .onDrop only to conclude it wasn’t what I wanted. Once I came across your video I made what I wanted in half the time and code. Thanks!
Very nice presentation! T😊hank you Nick. I added code to flip the up chevron to down when the view is at the top, based on endingOffsetY, passing the value into the MySignUp view as a @Binding var.
Amazing!!! Thank you so much for sharing. Quick question. Is the same if I use .offset(y: startingOffsetY + currentDragOffsetY + endingOffsetY) instead of three offsets modifiers?
This was great. Built a drag gesture based on this. However, if I touch the screen with a second finger while dragging the event ends and the view freezes in place. I notice that Apples apps, and others, that use a drag gesture continue the dragging even if multiple fingers touch it. It acts as if many fingers is just one finger. How do I do that?
Really enjoy this content. Will be adding support for drag gestures to my current project next week. One thing I’m curious about is how do you differentiate between single finger and multi finger drag operations?
@@stepanet I've got the same error, u may have named your file dragGesture so when u call dragGesture, swift calls your struct DragGesture and not the dragGesture you should name you file drag_Gesture
Great Bootcamp! I've been playing around. Now when I put more then one item on the screen, and I want to drag one of them, all items get dragged. So how would it work to have multiple items on the screen and want the to move them separately?
Hi Sandra! Instead of a single @State that hold coordinates for a single point, you could create an @State that holds an array of items and their associated drag values. Alternatively, you could stay with the single @State and add logic so that only the current item being dragged is moved
Great question! The UIScreen uses the bounds of the device in Portrait mode. So if a user rotates the device to landscape mode, the UIScreen.main.bounds.width does not update. However, the GeometryReader uses the bounds of the View itself. So if a user rotates the device to landscape, the geometry.size.width WILL update! However, the GeometryReader is more “expensive” and can slow down your app if you use it too often. My recommendation is to use the UIScreen when you don’t need Landscape mode, but GeometryReader when you do!
@@SwiftfulThinking the GR seems a bit more finicky too - you have to put it on each view, and when I tried to pass the values to an Environment class, it would not compile, where UIScreen seems pretty easy to use. Is there a UI attribute to tell you landscape or portrait mode?
Great tutorial 👍 How would you add a tap gesture to the final part along with the DragGesture() capability, if we wanted to enable the user to also be able to just tap the card so it bounces up?
Yea, you can use a UIPanGestureRecognizer and other gesture recognizers when using UIKit and Storyboard... however, it's MUCH harder to implement. Before SwiftUI was around, this was the only option lol.
Hi Nick! at the first sorry for my bad english, I have much questions for you 1)Screen.main is deprecated what can we use? 2) i am dont uderstant this moment, why we add "endindOffsetY" and why we do this "endingOffsetY = -startingOfssetY "
Great again! Is it possible to zoom an image inside a frame then drag around to get a closer look at various parts of the image ? Similar to what we see in those ecommerce sites. Do you have a tut for that? Thanks for your efforts man , much appreciated.
And what if I put scroll view in this kind of bottom sheet? how to work with scroll view + dragGesture? as the example - Apple Maps Bottom Sheets in iOS 15
You can put any content you want inside the View itself. You'll have to manage "simultaneous" gestures, but most of it should be done automatically for you.
Hi everyone. This is an interesting topic Do you think it is possible to limit the movement of the View only horizontally or vertically in the direction of the gesture? (In swiftUI) If the gesture to the left began, then continue moving only horizontally or vice versa.
Hey! Yes, this is totally possible. In the latter example of this video we used only the y offset, which limits the movement only vertically. You could use only the x offset if you wanted to go horizontally. Further, before updating the offset value within .onChange, you could first check if the new value is within the allowed area!
Hey, Nick Bro.. You forgot to do one simple thing. You forgot to add the following code that help prevent from scrolling upwards once you reached to the top level. if !(endingOffsetY == -startingOffsetY && value.translation.height < 0) { withAnimation(.spring()) { currentDragOffsetY = value.translation.height } }
.gesture( DragGesture() .onChanged { gesture in if gesture.translation.width > 0 && buttonOffset buttonWidth/2{ buttonOffset = buttonWidth-80 isOnboardingActive = false }else{ buttonOffset = 0 } } ) here isOnboarding is used to change the view of the screen by making it false when dragged to right but it is not reflecting any change.
Thanks again Nick. The math played with my head a little, but got there in the end!
Yea, the logic is tough the first time you do it haha. Luckily, once you do get it, it's pretty easy to customize it for projects.
I can't stress enough how important and helpful your content is! Thanks a lot for making these! I was kinda lost and couldn't structure my learning on SwiftUI, but your videos are just unbelievably well-structured... I want to say this again, thank you for your work and enthusiasm. Swift is actually fun, you make it fun, Nick! An awesome teaching.
Nice video!....Pretty soon at the rate your content is going...you'll be one of leading TH-cam channels for SwiftUI .....Good Luck!
Haha I'm just glad you're enjoying them! Thanks for the support David.
Thanks for dividing the video into two examples, both very useful.
I am creating a chess game and I wanted to be able to drag the pieces around to make moves. Spent lots of time trying to wrap my head around .onDrag and .onDrop only to conclude it wasn’t what I wanted. Once I came across your video I made what I wanted in half the time and code. Thanks!
Another great video with proper use cases
Perfect as always!
You have great tutorials buddy keep going
Appreciate the comment Kamil. Thanks for watching!
Very nice presentation! T😊hank you Nick. I added code to flip the up chevron to down when the view is at the top, based on endingOffsetY, passing the value into the MySignUp view as a @Binding var.
great info!
Beautiful!
Thanks Simon!
Amazing!!! Thank you so much for sharing. Quick question. Is the same if I use .offset(y: startingOffsetY + currentDragOffsetY + endingOffsetY) instead of three offsets modifiers?
I think that works lol
This video is very helpful for me. Thanks a lot!!
This was great. Built a drag gesture based on this. However, if I touch the screen with a second finger while dragging the event ends and the view freezes in place. I notice that Apples apps, and others, that use a drag gesture continue the dragging even if multiple fingers touch it. It acts as if many fingers is just one finger. How do I do that?
Thanks for perfect tutorial. Very useful.
Thanks Charlie!
Good lesson... Thank you
You're welcome. Happy to help!
Really enjoy this content. Will be adding support for drag gestures to my current project next week. One thing I’m curious about is how do you differentiate between single finger and multi finger drag operations?
Hi, the new version of Xcode warns to replace UIScreen.main.bounds ... can't figure out what to replace this code with?
Just perfect 👍
Love your tutotials
Thanks Justin!
Great tutorial Nick.
I want to know one thing:
What is the significance of using Double instead of CGFloat as the type of angle?
Hello Nick. This is not work in Xcode 12.5 Error: Instance method 'gesture(_:including:)' requires that 'some View' conform to 'Gesture'
Hi Dmitrii, I think you mistyped something or are using the wrong initializer for the gesture
@@SwiftfulThinking Can you help me write code in the new version of swift for an example ? stack overflow is empty.
@@stepanet I've got the same error, u may have named your file dragGesture so when u call dragGesture, swift calls your struct DragGesture and not the dragGesture you should name you file drag_Gesture
Nice tutorial. At first that offset.width threw me a little, like, how can an offset have a width? But then I understood your explanation. Thanks.
Yea, it's tricky at first but super useful!
Great Bootcamp! I've been playing around. Now when I put more then one item on the screen, and I want to drag one of them, all items get dragged. So how would it work to have multiple items on the screen and want the to move them separately?
Hi Sandra! Instead of a single @State that hold coordinates for a single point, you could create an @State that holds an array of items and their associated drag values. Alternatively, you could stay with the single @State and add logic so that only the current item being dragged is moved
Thank you so much for the Tutorial
whats the difference between using UIScreen and GeometryReader Proxy?
Great question! The UIScreen uses the bounds of the device in Portrait mode. So if a user rotates the device to landscape mode, the UIScreen.main.bounds.width does not update. However, the GeometryReader uses the bounds of the View itself. So if a user rotates the device to landscape, the geometry.size.width WILL update!
However, the GeometryReader is more “expensive” and can slow down your app if you use it too often. My recommendation is to use the UIScreen when you don’t need Landscape mode, but GeometryReader when you do!
@@SwiftfulThinking the GR seems a bit more finicky too - you have to put it on each view, and when I tried to pass the values to an Environment class, it would not compile, where UIScreen seems pretty easy to use. Is there a UI attribute to tell you landscape or portrait mode?
Great explenation... thank you very much
Great job, thank you!
you know update solution for .main deprecated ? what shold we can use neext ?
Great tutorial 👍 How would you add a tap gesture to the final part along with the DragGesture() capability, if we wanted to enable the user to also be able to just tap the card so it bounces up?
can we do the same things in UIKit or StoryBoard ??
Yea, you can use a UIPanGestureRecognizer and other gesture recognizers when using UIKit and Storyboard... however, it's MUCH harder to implement. Before SwiftUI was around, this was the only option lol.
Hi Nick! at the first sorry for my bad english, I have much questions for you
1)Screen.main is deprecated what can we use?
2) i am dont uderstant this moment, why we add "endindOffsetY" and why we do this "endingOffsetY = -startingOfssetY "
how can you restrict pop-up when it is already popped up?
It would be great to do a DragGesture from one stack to another
Great again! Is it possible to zoom an image inside a frame then drag around to get a closer look at various parts of the image ? Similar to what we see in those ecommerce sites. Do you have a tut for that? Thanks for your efforts man , much appreciated.
And what if I put scroll view in this kind of bottom sheet? how to work with scroll view + dragGesture? as the example - Apple Maps Bottom Sheets in iOS 15
You can put any content you want inside the View itself. You'll have to manage "simultaneous" gestures, but most of it should be done automatically for you.
Hi everyone. This is an interesting topic
Do you think it is possible to limit the movement of the View only horizontally or vertically in the direction of the gesture? (In swiftUI)
If the gesture to the left began, then continue moving only horizontally or vice versa.
Hey! Yes, this is totally possible. In the latter example of this video we used only the y offset, which limits the movement only vertically. You could use only the x offset if you wanted to go horizontally. Further, before updating the offset value within .onChange, you could first check if the new value is within the allowed area!
how can I make this work with geometry reader in the @ state
Thanks for your lessons! You are the best teacher!!!
Where is the swipe explanation?
I want same thing in swift code?
This would look great if the green screen contained a screen for logging in. Then the user could swipe up if they didn’t have an account. Thank you.
That's definitely one of the most common use cases these days. All the major social media apps are adding small popups with drag gestures!
amazing
Hey, Nick Bro.. You forgot to do one simple thing.
You forgot to add the following code that help prevent from scrolling upwards once you reached to the top level.
if !(endingOffsetY == -startingOffsetY && value.translation.height < 0) {
withAnimation(.spring()) {
currentDragOffsetY = value.translation.height
}
}
the most difficult lesson
Good!!
Faster! haha, we just got out of Bootcamp...
.gesture(
DragGesture()
.onChanged { gesture in
if gesture.translation.width > 0 && buttonOffset buttonWidth/2{
buttonOffset = buttonWidth-80
isOnboardingActive = false
}else{
buttonOffset = 0
}
}
)
here isOnboarding is used to change the view of the screen by making it false when dragged to right but it is not reflecting any change.