偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

探索 SwiftUI 基本手勢(shì)

開發(fā) 前端
本篇是對(duì) SwiftUI 基本手勢(shì)的總結(jié)。我們可以實(shí)現(xiàn)更多的交互使我們的 App 變得更生動(dòng)。對(duì)于高級(jí)的使用,可以將手勢(shì)組合或者同時(shí)使用以做出響應(yīng),或者可以實(shí)現(xiàn)自己的自定義手勢(shì)。

[[400610]]

前言

在 SwiftUI 中,我們可以通過添加不同的交互來使我們的應(yīng)用程序更具交互性,這些交互可以響應(yīng)我們的點(diǎn)擊,點(diǎn)擊和滑動(dòng)。

今天,我們將回顧SwiftUI基本手勢(shì):

  • TapGesture
  • 長按手勢(shì)
  • 拖動(dòng)手勢(shì)
  • 放大手勢(shì)
  • 旋轉(zhuǎn)手勢(shì)

TapGesture

輕擊手勢(shì)使我們能夠識(shí)別 View 上的一個(gè)或多個(gè)輕擊。我們有幾種方法可以添加點(diǎn)擊手勢(shì)。

第一個(gè)是直接使用 .onTapGesture 修飾符。

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

SwiftUI 文檔中使用的其他選項(xiàng)是通過創(chuàng)建手勢(shì)并將其配置為屬性,然后將其與 .gesture(_:include :) 修飾符一起使用。

注意: 為了執(zhí)行某項(xiàng)操作或響應(yīng)輕擊,我們需要使用 .onEnded 操作關(guān)閉,該操作在手勢(shì)結(jié)束時(shí)觸發(fā)。

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

實(shí)際上,我更喜歡第二種方法,因?yàn)檫@樣我們可以創(chuàng)建不同的手勢(shì)并通過我們的代碼重復(fù)使用它們。

因此,如果我們將代碼放在一起,就可以開始編寫類似的東西。

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

類似地,我們只需使用 TapGesture(count:Int) 初始化程序就可以控制要響應(yīng)的數(shù)量。

在這種情況下,您需要點(diǎn)擊3次才能觸發(fā) .onEnded 操作關(guān)閉。

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

長按手勢(shì)

長按手勢(shì)可讓我們?cè)谟脩糸L按定義的時(shí)間后以及在用戶長按的時(shí)間內(nèi)執(zhí)行操作。

我們可以設(shè)置一個(gè)最小持續(xù)時(shí)間,以識(shí)別我們的長按手勢(shì)??梢栽?LongPressGesture 初始化程序中進(jìn)行設(shè)置。

  1. LongPressGesture(minimumDuration: 2) 

然后,我們可以使用 .updating 方法在長按期間執(zhí)行操作,并使用 .onEnded 在識(shí)別到我們的手勢(shì)時(shí)執(zhí)行操作。

在此示例中,我將在長按操作期間更新 Circle() 的大小和顏色,并且當(dāng)識(shí)別出手勢(shì)時(shí),我將顯示“文本已完成”。

另外,我在這里使用的是 GestureState 屬性包裝器,該包裝器在長按期間設(shè)置為 true ,在手勢(shì)結(jié)束時(shí)設(shè)置為 false 。我正在將此屬性包裝器用于示例動(dòng)畫。

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

 拖動(dòng)手勢(shì)

拖動(dòng)手勢(shì)允許我們?cè)谕蟿?dòng)視圖時(shí)執(zhí)行操作。

我們可以利用并使用 .onChanged 和 .onEnded 關(guān)閉方法來執(zhí)行某些操作。這兩種方法都為我們提供了出色的屬性 DragGesture.Value,該屬性存儲(chǔ)以下拖動(dòng)動(dòng)作信息:

location

predictedEndLocation

predictedEndTranslation

startLocation

time

translation

我們可以使用該屬性來創(chuàng)建可移動(dòng)視圖。在當(dāng)前示例中,我使用 .onChanged 方法更新 Circle() 位置坐標(biāo)。

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

在這里,添加了 .onEnded 方法,以在拖動(dòng)結(jié)束后重置 Circle() 位置坐標(biāo)。

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

放大手勢(shì)

當(dāng)我們?cè)赩iew上應(yīng)用放大動(dòng)作時(shí),放大手勢(shì)允許做出一些動(dòng)作。

在這里,還有 .onChanged 和 .onEnded 閉包,我們可以使用它們來在放大動(dòng)作期間或結(jié)束時(shí)進(jìn)行響應(yīng)。作為屬性,接收到的是 CGFloat 的 MagnificationGesture.Value 。我們可以以此為例來更改視圖大小。

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

旋轉(zhuǎn)手勢(shì)

旋轉(zhuǎn)手勢(shì)允許旋轉(zhuǎn)視圖,并在旋轉(zhuǎn)過程中和旋轉(zhuǎn)結(jié)束時(shí)以某些動(dòng)作做出響應(yīng)。

它還為我們提供了 .onChanged 和 .onEnded 閉包,這些閉包為我們提供了 RotationGesture.Value,它表示手勢(shì) Angle 值。我們可以使用該值旋轉(zhuǎn)視圖。

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

總結(jié)

上面是對(duì) SwiftUI 基本手勢(shì)的總結(jié)。我們可以實(shí)現(xiàn)更多的交互使我們的 App 變得更生動(dòng)。

對(duì)于高級(jí)的使用,可以將手勢(shì)組合或者同時(shí)使用以做出響應(yīng),或者可以實(shí)現(xiàn)自己的自定義手勢(shì)。

 

責(zé)任編輯:姜華 來源: Swift 社區(qū)
相關(guān)推薦

2011-06-28 09:53:43

iPhone諾基亞N9

2015-07-22 10:34:59

手勢(shì)密碼源碼

2013-05-14 11:18:24

AIR AndroidSwipe手勢(shì)

2022-02-18 09:04:22

動(dòng)畫SwiftUI工具:

2022-02-23 12:23:28

協(xié)議動(dòng)畫SwiftUI

2022-03-01 09:01:56

SwiftUI動(dòng)畫進(jìn)階Canvas

2016-03-16 09:40:33

Windows 10觸控手勢(shì)微軟

2022-06-20 09:01:50

SwiftUI狀態(tài)管理系統(tǒng)

2022-08-24 09:02:27

SwiftUIiOS

2022-03-09 09:00:41

SwiftUI視圖生成器Swift

2010-09-28 15:09:10

2013-05-14 11:16:26

AIR Android旋轉(zhuǎn)手勢(shì)

2013-06-20 10:50:51

Objective-CiOS左右滑動(dòng)手勢(shì)

2024-06-20 11:11:07

2022-12-19 09:02:04

深入布局協(xié)議HStack

2013-07-18 18:14:26

UITableViewiOS長按手勢(shì)UILongPress

2022-05-17 12:25:59

物聯(lián)網(wǎng)智能建筑樓宇自控

2023-12-29 09:01:10

SwiftUI空狀態(tài)Product?

2021-07-13 12:20:40

Core DataSwiftUIiOS

2022-02-14 09:24:15

SwiftUI協(xié)議
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)