[SwiftUI] Gesture và Animation trong SwiftUI

Tram Ho

Attaching gestures for processing in SwiftUI is a little different from UIKit.

In this section, we will talk about DragGesture, LongPressGesture, TapGesture and RotationGesture.

We will implement all of these gestures in the sample application, and we will also look at how these gestures work.

. . .

Begin

Open Xcode and create a new Project.

Select Single View App and click Next.

Enter Project name and select SwiftUI in User Interface -> Click Next.

By default, SwiftUI declares two structures. The first structure conforms to the View protocol that describes the content and layout of the views. The second structure creates a preview for that view.

Perform

We start by creating a TapGesture. TapGesture is called when the user touches View.

First press Command + N to create a new SwiftUI View file. Go to the TapGestureView file and paste the following code into TapGestureView.

In the above code, we have an @State changeColor variable to change the color of Circle () after each tap.

Then we created a Circle (), set the foregroundColor to the @State changeColor variable and finally, we added gestures and default animations.

Build and Run

It will look like this.

So after TapGesture, our next gesture is LongPressGesture. LongPressGesture is called when we press and hold on View.

First press Command + N to create a new SwiftUI View file. Call the LongPressGestureView file and paste the following code into LongPressGestureView.

In the code above, we first have three variables @State didLongPress, scale and color. Then create Circle () and in scaleEffect pass on scale to enlarge Circle ().

After that, we set the frame and in the foregroundColor change the color to change the color after each LongPress, then set some default animations.

Build and Run

It will look as above.

Now we will perform DragGesture. DragGesture is called when we drag something like circles, rectangles, images, etc.

Now press Command + N to create a new SwiftUI View file. Call the DragGestureView file and paste the following code into DragGestureView.

In the code above, we have two drag and color variables. After that, create Circle () and in foregroundColor pass color, Circle () will change color in color after dragging Circle ().

Then assign drag to the offset property to drag Circle () and add some default animations and finally we set DragGesture.

Build and Run

It looks like this.

The final gesture is RoatationGesture. RoatationGesture is called when we rotate something such as circles, rectangles, images, etc.

Now press Command + N to create a new SwiftUI View file. Call the RoatationGestureView file and paste the following code into RoatationGestureView.

In the above code, we first have two rotation and color variables, then create RoatationGesture, set Rectangle () and pass the color to the foregroundColor to change the Rectangle () color and in the rotationEffect pass to rotation and finally, we have Add some default animations.

Build and Run

The result will be like this. Thank you for watching all.

Source: Medium.com

Share the news now

Source : Viblo