探索SwiftUI基本手势

前言

在 SwiftUI 中,我们可以通过添加不同的交互来使我们的应用程序更具交互性,这些交互可以响应我们的点击,点击和滑动。

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、成都微信小程序、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了山阳免费建站欢迎大家使用!

今天,我们将回顾SwiftUI基本手势:

  • TapGesture
  • 长按手势
  • 拖动手势
  • 放大手势
  • 旋转手势

TapGesture

轻击手势使我们能够识别 View 上的一个或多个轻击。我们有几种方法可以添加点击手势。

第一个是直接使用 .onTapGesture 修饰符。

 
 
 
 
  1. Circle()
  2.   .onTapGesture {
  3.     // Respond to Tap Gesture 
  4.   }

SwiftUI 文档中使用的其他选项是通过创建手势并将其配置为属性,然后将其与 .gesture(_:include ???? 修饰符一起使用。

注意: 为了执行某项操作或响应轻击,我们需要使用 .onEnded 操作关闭,该操作在手势结束时触发。

 
 
 
 
  1. struct SingleTapGestureView: View {
  2.   var singleTap: some Gesture {
  3.       TapGesture()
  4.           .onEnded { _ in
  5.               // Respond to Tap Gesture
  6.           }
  7.   }
  8.   var body: some View {
  9.       Circle()
  10.           .gesture(singleTap)
  11.   }
  12. }

实际上,我更喜欢第二种方法,因为这样我们可以创建不同的手势并通过我们的代码重复使用它们。

因此,如果我们将代码放在一起,就可以开始编写类似的东西。

 
 
 
 
  1. struct TapGestureView: View {
  2.     @State private var isAnimating = false
  3.     @State private var tapped1x = 0
  4.     var singleTap: some Gesture {
  5.         TapGesture()
  6.             .onEnded { _ in
  7.                 tapped1x += 1
  8.                 withAnimation(Animation.easeOut(duration: 0.5)) {
  9.                     self.isAnimating = true
  10.                 }
  11.                 DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  12.                     self.isAnimating = false
  13.                 }
  14.             }
  15.     }
  16.     var body: some View {
  17.         VStack {
  18.             Text("Tapped 1X: \(tapped1x) times")
  19.                 .font(.caption)
  20.             Circle()
  21.                 .frame(width: 80, height: 80)
  22.                 .foregroundColor(.orange)
  23.                 .overlay(
  24.                     Text("1X")
  25.                         .fontWeight(.medium)
  26.                 )
  27.                 .background(
  28.                     Circle()
  29.                         .strokeBorder(Color.blue, lineWidth: 3)
  30.                         .scaleEffect(isAnimating ? 1.5 : 1)
  31.                         .opacity(isAnimating ? 0 : 1)
  32.                 )
  33.                 .gesture(singleTap)
  34.         }
  35.     }
  36. }

类似地,我们只需使用 TapGesture(count:Int) 初始化程序就可以控制要响应的数量。

在这种情况下,您需要点击3次才能触发 .onEnded 操作关闭。

 
 
 
 
  1. struct TapGesture3xView: View {
  2.     @State private var isAnimating = false
  3.     @State private var tapped3x = 0
  4.     var multipleTap: some Gesture {
  5.         TapGesture(count: 3)
  6.             .onEnded { _ in
  7.                 tapped3x += 1
  8.                 withAnimation(Animation.easeOut(duration: 0.5)) {
  9.                     self.isAnimating = true
  10.                 }
  11.                 DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  12.                     self.isAnimating = false
  13.                 }
  14.             }
  15.     }
  16.     var body: some View {
  17.         VStack {
  18.             Text("Tapped 3X: \(tapped3x) times")
  19.                 .font(.caption)
  20.             Circle()
  21.                 .frame(width: 80, height: 80)
  22.                 .foregroundColor(.orange)
  23.                 .overlay(
  24.                     Text("3X")
  25.                         .fontWeight(.medium)
  26.                 )
  27.                 .background(
  28.                     Circle()
  29.                         .strokeBorder(Color.blue, lineWidth: 3)
  30.                         .scaleEffect(isAnimating ? 1.5 : 1)
  31.                         .opacity(isAnimating ? 0 : 1)
  32.                 )
  33.                 .gesture(multipleTap)
  34.         }
  35.     }
  36. }

长按手势

长按手势可让我们在用户长按定义的时间后以及在用户长按的时间内执行操作。

我们可以设置一个最小持续时间,以识别我们的长按手势。可以在 LongPressGesture 初始化程序中进行设置。

 
 
 
 
  1. LongPressGesture(minimumDuration: 2)

然后,我们可以使用 .updating 方法在长按期间执行操作,并使用 .onEnded 在识别到我们的手势时执行操作。

在此示例中,我将在长按操作期间更新 Circle() 的大小和颜色,并且当识别出手势时,我将显示“文本已完成”。

另外,我在这里使用的是 GestureState 属性包装器,该包装器在长按期间设置为 true ,在手势结束时设置为 false 。我正在将此属性包装器用于示例动画。

 
 
 
 
  1. struct LongPressGestureView: View {
  2.     @GestureState private var isLongPressDetected = false
  3.     @State private var isDone = false
  4.     var longPress: some Gesture {
  5.         LongPressGesture(minimumDuration: 2)
  6.             .updating($isLongPressDetected) { currentState, gestureState, transaction in
  7.                 DispatchQueue.main.async {
  8.                     isDone = false
  9.                 }
  10.                 gestureState = currentState
  11.                 transaction.animation = Animation.easeIn(duration: 2)
  12.             }
  13.             .onEnded { done in
  14.                 isDone = done
  15.             }
  16.     }
  17.     var body: some View {
  18.         VStack {
  19.             Spacer()
  20.             Circle()
  21.                 .frame(width: 10, height: 10)
  22.                 .foregroundColor(isLongPressDetected ? .orange : .primary)
  23.                 .scaleEffect(CGSize(
  24.                                 width: isLongPressDetected ? 10 : 1,
  25.                                 height: isLongPressDetected ? 10 : 1))
  26.             Spacer()
  27.             if isLongPressDetected {
  28.                 Text("Updating...")
  29.             }
  30.             if isDone {
  31.                 Text("Done")
  32.             }
  33.             Spacer()
  34.             Text("Long Press 2 sec")
  35.                 .padding()
  36.                 .background(isLongPressDetected ? Color.green : Color.orange)
  37.                 .cornerRadius(16)
  38.                 .gesture(longPress)
  39.         }
  40.     }
  41. }

 拖动手势

拖动手势允许我们在拖动视图时执行操作。

我们可以利用并使用 .onChanged 和 .onEnded 关闭方法来执行某些操作。这两种方法都为我们提供了出色的属性 DragGesture.Value,该属性存储以下拖动动作信息:

location

predictedEndLocation

predictedEndTranslation

startLocation

time

translation

我们可以使用该属性来创建可移动视图。在当前示例中,我使用 .onChanged 方法更新 Circle() 位置坐标。

 
 
 
 
  1. struct DragGestureView: View {
  2.     @State private var location: CGPoint = CGPoint(x: 100, y: 100)
  3.     var drag: some Gesture {
  4.         DragGesture(minimumDistance: 1, coordinateSpace: .local)
  5.             .onChanged { value in
  6.                 location = value.location
  7.             }
  8.     }
  9.     var body: some View {
  10.         Circle()
  11.             .frame(width: 100, height: 100)
  12.             .foregroundColor(.orange)
  13.             .position(location)
  14.             .gesture(drag)
  15.     }
  16. }

在这里,添加了 .onEnded 方法,以在拖动结束后重置 Circle() 位置坐标。

 
 
 
 
  1. struct DragGestureView: View {
  2.     @State private var location: CGPoint = CGPoint(x: 100, y: 100)
  3.     var drag: some Gesture {
  4.         DragGesture(minimumDistance: 1, coordinateSpace: .local)
  5.             .onChanged { value in
  6.                 location = value.location
  7.             }
  8.             .onEnded { value in
  9.                 withAnimation(.easeOut) {
  10.                     location = CGPoint(x: 100, y: 100)
  11.                 }
  12.             }
  13.     }
  14.     var body: some View {
  15.         Circle()
  16.             .frame(width: 100, height: 100)
  17.             .foregroundColor(.orange)
  18.             .position(location)
  19.             .gesture(drag)
  20.     }
  21. }

放大手势

当我们在View上应用放大动作时,放大手势允许做出一些动作。

在这里,还有 .onChanged 和 .onEnded 闭包,我们可以使用它们来在放大动作期间或结束时进行响应。作为属性,接收到的是 CGFloat 的 MagnificationGesture.Value 。我们可以以此为例来更改视图大小。

 
 
 
 
  1. struct MagnificationGestureView: View {
  2.     @State var magnifiedValue: CGFloat = 1.0
  3.     var magnification: some Gesture {
  4.         MagnificationGesture()
  5.             .onChanged { value in
  6.                 magnifiedValue = value
  7.             }
  8.             .onEnded { value in
  9.                 magnifiedValue = 1.0
  10.             }
  11.     }
  12.     var body: some View {
  13.         Circle()
  14.             .frame(width: 100 * magnifiedValue, height: 100 * magnifiedValue)
  15.             .foregroundColor(.orange)
  16.             .gesture(magnification)
  17.             .animation(.easeOut)
  18.     }
  19. }

旋转手势

旋转手势允许旋转视图,并在旋转过程中和旋转结束时以某些动作做出响应。

它还为我们提供了 .onChanged 和 .onEnded 闭包,这些闭包为我们提供了 RotationGesture.Value,它表示手势 Angle 值。我们可以使用该值旋转视图。

 
 
 
 
  1. struct RotationGestureView: View {
  2.     @State private var angle = Angle(degrees: 0.0)
  3.     @State private var backgroundAngle = Angle(degrees: 0.0)
  4.     var rotation: some Gesture {
  5.         RotationGesture()
  6.             .onChanged { angle in
  7.                 self.angle = angle
  8.             }
  9.             .onEnded { angle in
  10.                 withAnimation(Animation.spring()) {
  11.                     self.backgroundAngle = angle
  12.                 }
  13.             }
  14.     }
  15.     var body: some View {
  16.         Rectangle()
  17.             .frame(width: 150, height: 150, alignment: .center)
  18.             .foregroundColor(.orange)
  19.             .rotationEffect(self.angle)
  20.             .gesture(rotation)
  21.             .background(
  22.                 Rectangle()
  23.                     .shadow(color: .primary, radius: 10, x: 0.0, y: 0.01)
  24.                     .foregroundColor(.secondary)
  25.                     .rotationEffect(backgroundAngle)
  26.             )
  27.     }
  28. }

总结

上面是对 SwiftUI 基本手势的总结。我们可以实现更多的交互使我们的 App 变得更生动。

对于高级的使用,可以将手势组合或者同时使用以做出响应,或者可以实现自己的自定义手势。

文章标题:探索SwiftUI基本手势
浏览路径:http://www.shufengxianlan.com/qtweb/news7/494307.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联