#1 onLongPressGesture

功能

为任意 View 添加长按手势识别。当用户持续按压且达到指定时长、同时手指偏移不超过阈值时,视为一次有效长按;可实时获取按压状态以驱动过渡动画。

参数说明

  • minimumDuration:触发所需最短按压时间(秒)。
  • maximumDistance:手指允许的最大偏移,单位为点;超限即判定为取消。
  • onPressingChanged:按压状态变化回调;true 表示按下,false 表示抬起或滑出。
  • action:满足时长与偏移条件后执行的一次性回调。

代码示例

swift 复制代码
struct LongPressGestureBootcamp: View {
    
    @State var isComplete: Bool = false
    @State var isSuccess: Bool = false
    var body: some View {
        
        VStack {
            Rectangle()
                .fill(isSuccess ? .green : .blue)
                .frame(maxWidth: isComplete ? .infinity : 0)
                .frame(height: 56)
                .frame(maxWidth: .infinity, alignment: .leading)
                .background(.gray)
            
            HStack {
                Text("CLICK HERE")
                    .foregroundStyle(.white)
                    .padding()
                    .background(.black)
                    .cornerRadius(8)
                    .onLongPressGesture(
                        minimumDuration: 1.0,
                        maximumDistance: 56) { (isPressing) in
                            // start of press -> min duration
                            if isPressing {
                                withAnimation(.easeInOut(duration: 1.0)) {
                                    isComplete = true
                                }
                            }
                            else {
                                DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                                    if !isSuccess {
                                        withAnimation(.easeInOut) {
                                            isComplete = false
                                        }
                                    }
                                }
                            }
                        } perform: {
                            // at the min duration
                            withAnimation(.easeInOut) {
                                isSuccess = true
                            }
                        }
                
                Text("RESET")
                    .foregroundStyle(.white)
                    .padding()
                    .background(.black)
                    .cornerRadius(8)
                    .onTapGesture {
                        isComplete = false;
                        isSuccess = false;
                    }
            }
        }
        
        
//        Text(isComplete ? "COMPLETED" : "NOT COMPLETE")
//            .padding()
//            .background(isComplete ? .green : .gray)
//            .cornerRadius(8)
////            .onTapGesture {
////                withAnimation {
////                    isComplete.toggle()
////                }
////            }
//            .onLongPressGesture(minimumDuration: 1.0, maximumDistance: 50, perform: {
//                isComplete.toggle()
//            })
    }
}

注意事项

  1. 若同时附加 .onTapGesture,长按结束后可能额外触发一次点按,应通过状态标志互斥。
  2. onPressingChanged 中更新界面时,请使用 withAnimation 保证过渡流畅。
  3. 耗时操作请置于 action 的异步闭包内,避免阻塞主线程。
相关推荐
茶底世界之下20 小时前
为什么预览、录制、离线导出不能共享同一个背压策略?
ios·swift
2501_9160074721 小时前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
2501_916008891 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
2501_915909061 天前
移动端开发工具链怎么组合比较好,iOS、Android、跨端三套配置方案
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
东坡肘子1 天前
一场关于 SwiftUI 动画的“原生”之争 -- 肘子的 Swift 周报 #154
人工智能·swiftui·swift
2501_916008891 天前
怎么用 Godot 导出 iOS 应用并上架 App Store?签名字段该填什么?
android·ios·小程序·https·uni-app·iphone·webview
2501_915918411 天前
iOS编程用什么软件好?主流工具Xcode、AppCode、CodeRunner与新兴快蝎对比
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
终端安全笔记2 天前
iOS 27 之后「策略空转」:设备升级不报错,但旧策略不再管它
android·网络·安全·ios·智能手机
9765033352 天前
iOS 上架 4.3a 被拒【uniapp专讲】
flutter·ios·objective-c·uniapp·swift
黑化旺仔3 天前
【OC】KVO
macos·ios·objective-c·cocoa