{"id":4938,"date":"2022-01-19T11:29:44","date_gmt":"2022-01-19T03:29:44","guid":{"rendered":"http:\/\/123.57.164.21\/?p=4938"},"modified":"2022-01-19T11:29:44","modified_gmt":"2022-01-19T03:29:44","slug":"%e5%9c%a8-swiftui-%e4%b8%8b%e5%ae%9a%e5%88%b6%e6%89%8b%e5%8a%bf","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=4938","title":{"rendered":"\u5728 SwiftUI \u4e0b\u5b9a\u5236\u624b\u52bf"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1ahttps:\/\/www.fatbobman.com\/posts\/swiftuiGesture\/<\/p>\n\n\n\n<p>\u4e0d\u540c\u4e8e\u4f17\u591a\u7684\u5185\u7f6e\u63a7\u4ef6\uff0cSwiftUI \u6ca1\u6709\u91c7\u7528\u5bf9 UIGestureRecognizer\uff08\u6216 NSGestureRecognizer\uff09\u8fdb\u884c\u5305\u88c5\u7684\u5f62\u5f0f\uff0c\u800c\u662f\u91cd\u6784\u4e86\u81ea\u5df1\u7684\u624b\u52bf\u4f53\u7cfb\u3002SwiftUI \u624b\u52bf\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u964d\u4f4e\u4e86\u4f7f\u7528\u95e8\u69db\uff0c\u4f46\u7531\u4e8e\u7f3a\u4e4f\u63d0\u4f9b\u5e95\u5c42\u6570\u636e\u7684 API\uff0c\u4e25\u91cd\u5236\u7ea6\u4e86\u5f00\u53d1\u8005\u7684\u6df1\u5ea6\u5b9a\u5236\u80fd\u529b\u3002\u5728 SwiftUI \u4e0b\uff0c\u6211\u4eec\u65e0\u6cd5\u62e5\u6709\u7c7b\u4f3c\u6784\u5efa\u5168\u65b0 UIGestureRecongnizer \u7684\u80fd\u529b\u3002\u6240\u8c13\u7684\u81ea\u5b9a\u4e49\u624b\u52bf\uff0c\u5176\u5b9e\u53ea\u662f\u5bf9\u7cfb\u7edf\u9884\u7f6e\u624b\u52bf\u7684\u91cd\u6784\u800c\u5df2\u3002\u672c\u6587\u5c06\u901a\u8fc7\u51e0\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 SwiftUI \u63d0\u4f9b\u7684\u539f\u751f\u624b\u6bb5\u5b9a\u5236\u6240\u9700\u624b\u52bf\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u57fa\u7840<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>\u9884\u7f6e\u624b\u52bf<\/strong><\/p>\n\n\n\n<p>SwiftUI \u76ee\u524d\u63d0\u4f9b\u4e86 5 \u79cd\u9884\u7f6e\u624b\u52bf\uff0c\u5206\u522b\u4e3a\u70b9\u51fb\u3001\u957f\u6309\u3001\u62d6\u62fd\u3001\u7f29\u653e\u548c\u65cb\u8f6c\u3002\u50cf<code>onTapGesture<\/code>\u4e4b\u7c7b\u7684\u8c03\u7528\u65b9\u5f0f\uff0c\u5b9e\u9645\u4e0a\u662f\u4e3a\u4e86\u4fbf\u6377\u800c\u521b\u5efa\u7684\u89c6\u56fe\u6269\u5c55\u3002<\/p>\n\n\n\n<ul><li>\u70b9\u51fb\uff08TapGesture\uff09<\/li><\/ul>\n\n\n\n<p>\u53ef\u8bbe\u5b9a\u70b9\u51fb\u6b21\u6570\uff08\u5355\u51fb\u3001\u53cc\u51fb\uff09\u3002\u662f\u4f7f\u7528\u9891\u7387\u6700\u9ad8\u7684\u624b\u52bf\u4e4b\u4e00\u3002<\/p>\n\n\n\n<ul><li>\u957f\u6309\uff08LongPressGesture\uff09<\/li><\/ul>\n\n\n\n<p>\u5f53\u6309\u538b\u6ee1\u8db3\u4e86\u8bbe\u5b9a\u65f6\u957f\u540e\uff0c\u53ef\u89e6\u53d1\u6307\u5b9a\u95ed\u5305\u3002<\/p>\n\n\n\n<ul><li>\u62d6\u62fd\uff08DragGesture\uff09<\/li><\/ul>\n\n\n\n<p>SwiftUI \u5c06 Pan \u548c Swipe \u5408\u4e8c\u4e3a\u4e00\uff0c\u4f4d\u7f6e\u53d8\u5316\u65f6\uff0c\u63d0\u4f9b\u62d6\u52a8\u6570\u636e\u3002<\/p>\n\n\n\n<ul><li>\u7f29\u653e\uff08MagnificationGesture\uff09<\/li><\/ul>\n\n\n\n<p>\u4e24\u6307\u7f29\u653e\u3002<\/p>\n\n\n\n<ul><li>\u65cb\u8f6c\uff08RotationGesture\uff09<\/li><\/ul>\n\n\n\n<p>\u4e24\u6307\u65cb\u8f6c\u3002<\/p>\n\n\n\n<p>\u70b9\u51fb\u3001\u957f\u6309\u3001\u62d6\u62fd\u4ec5\u652f\u6301\u5355\u6307\u3002SwiftUI \u6ca1\u6709\u63d0\u4f9b\u624b\u6307\u6570\u8bbe\u5b9a\u529f\u80fd\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u9664\u4e86\u4e0a\u8ff0\u63d0\u4f9b\u7ed9\u5f00\u53d1\u8005\u4f7f\u7528\u7684\u624b\u52bf\u5916\uff0cSwiftUI \u5176\u5b9e\u8fd8\u6709\u5927\u91cf\u7684\u5185\u90e8\uff08\u975e\u516c\u5f00\uff09\u624b\u52bf\u7ed9\u7cfb\u7edf\u63a7\u4ef6\u4f7f\u7528\uff0c\u4f8b\u5982\uff1aScrollGesture\u3001_ButtonGesture \u7b49\u3002<\/p><\/blockquote>\n\n\n\n<p><strong>Value<\/strong><\/p>\n\n\n\n<p>SwiftUI \u4f1a\u4f9d\u636e\u624b\u52bf\u7684\u7c7b\u578b\u63d0\u4f9b\u4e0d\u540c\u7684\u6570\u636e\u5185\u5bb9\u3002<\/p>\n\n\n\n<ul><li>\u70b9\u51fb\uff1a\u6570\u636e\u7c7b\u578b\u4e3a Void<\/li><li>\u957f\u6309\uff1a\u6570\u636e\u7c7b\u578b\u4e3a Bool\uff0c\u5f00\u59cb\u6309\u538b\u540e\u63d0\u4f9b true<\/li><li>\u62d6\u62fd\uff1a\u63d0\u4f9b\u4e86\u6700\u5168\u9762\u7684\u6570\u636e\u4fe1\u606f\uff0c\u5305\u542b\u5f53\u524d\u4f4d\u7f6e\u3001\u504f\u79fb\u91cf\u3001\u4e8b\u4ef6\u65f6\u95f4\u3001\u9884\u6d4b\u7ec8\u70b9\u3001\u9884\u6d4b\u504f\u79fb\u91cf\u7b49\u5185\u5bb9<\/li><li>\u7f29\u653e\uff1a\u6570\u636e\u7c7b\u578b\u4e3a CGFloat\uff0c\u7f29\u653e\u91cf<\/li><li>\u65cb\u8f6c\uff1a\u6570\u636e\u7c7b\u578b\u4e3a Angle\uff0c\u65cb\u8f6c\u89d2\u5ea6<\/li><\/ul>\n\n\n\n<p>\u4f7f\u7528<code>map<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u624b\u52bf\u63d0\u4f9b\u7684\u6570\u636e\u8f6c\u6362\u6210\u5176\u4ed6\u7684\u7c7b\u578b\uff0c\u65b9\u4fbf\u4e4b\u540e\u7684\u8c03\u7528\u3002<\/p>\n\n\n\n<p><strong>\u65f6\u673a<\/strong><\/p>\n\n\n\n<p>SwiftUI \u624b\u52bf\u5185\u90e8\u6ca1\u6709\u72b6\u6001\u4e00\u8bf4\uff0c\u901a\u8fc7\u8bbe\u7f6e\u4e0e\u6307\u5b9a\u65f6\u673a\u5bf9\u5e94\u7684\u95ed\u5305\uff0c\u624b\u52bf\u4f1a\u5728\u9002\u5f53\u7684\u65f6\u673a\u81ea\u52a8\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n\n\n\n<ul><li>onEnded<\/li><\/ul>\n\n\n\n<p>\u5728\u624b\u52bf\u7ed3\u675f\u65f6\u6267\u884c\u7684\u64cd\u4f5c<\/p>\n\n\n\n<ul><li>onChanged<\/li><\/ul>\n\n\n\n<p>\u5f53\u624b\u52bf\u63d0\u4f9b\u7684\u503c\u53d1\u751f\u53d8\u5316\u65f6\u6267\u884c\u7684\u64cd\u4f5c\u3002\u53ea\u5728 Value \u7b26\u5408 Equatable \u65f6\u63d0\u4f9b\uff0c\u56e0\u6b64 TapGesture \u4e0d\u652f\u6301\u3002<\/p>\n\n\n\n<ul><li>updating<\/li><\/ul>\n\n\n\n<p>\u6267\u884c\u65f6\u673a\u540c onChanged \u76f8\u540c\u3002\u5bf9 Value \u6ca1\u6709\u7279\u522b\u7ea6\u5b9a\uff0c\u76f8\u8f83 onChanged \uff0c\u589e\u52a0\u4e86\u66f4\u65b0\u624b\u52bf\u5c5e\u6027\uff08GestureState\uff09\u548c\u83b7\u53d6 Transaction \u7684\u80fd\u529b\u3002<\/p>\n\n\n\n<p>\u4e0d\u540c\u7684\u624b\u52bf\uff0c\u5bf9\u65f6\u673a\u7684\u5173\u6ce8\u70b9\u6709\u6240\u533a\u522b\u3002\u70b9\u51fb\u901a\u5e38\u53ea\u5173\u6ce8 onEnded\uff1bonChanged\uff08\u6216 updating\uff09\u5728\u62d6\u62fd\u3001\u7f29\u653e\u3001\u65cb\u8f6c\u4e2d\u4f5c\u7528\u66f4\u5927\uff1b\u957f\u6309\u53ea\u6709\u5728\u6ee1\u8db3\u4e86\u8bbe\u5b9a\u65f6\u957f\u7684\u60c5\u51b5\u4e0b\uff0c\u624d\u4f1a\u8c03\u7528 onEnded\u3002<\/p>\n\n\n\n<p><strong>GestureState<\/strong><\/p>\n\n\n\n<p>\u95e8\u4e3a SwiftUI \u624b\u52bf\u5f00\u53d1\u7684\u5c5e\u6027\u5305\u88c5\u5668\u7c7b\u578b\uff0c\u53ef\u4f5c\u4e3a\u4f9d\u8d56\u9879\u9a71\u52a8\u89c6\u56fe\u66f4\u65b0\u3002\u76f8\u8f83 State \u6709\u5982\u4e0b\u4e0d\u540c\uff1a<\/p>\n\n\n\n<ul><li>\u53ea\u80fd\u5728\u624b\u52bf\u7684 updating \u65b9\u6cd5\u4e2d\u4fee\u6539\uff0c\u5728\u89c6\u56fe\u5176\u5b83\u7684\u5730\u65b9\u4e3a\u53ea\u8bfb<\/li><li>\u5728\u624b\u52bf\u7ed3\u675f\u65f6\uff0c\u4e0e\u4e4b\u5173\u8054\uff08\u4f7f\u7528 updating \u8fdb\u884c\u5173\u8054\uff09\u7684\u624b\u52bf\u4f1a\u81ea\u52a8\u5c06\u5176\u5185\u5bb9\u6062\u590d\u5230\u5b83\u7684\u521d\u59cb\u503c<\/li><li>\u901a\u8fc7 resetTransaction \u53ef\u4ee5\u8bbe\u7f6e\u6062\u590d\u521d\u59cb\u6570\u636e\u65f6\u7684\u52a8\u753b\u72b6\u6001<\/li><\/ul>\n\n\n\n<p><strong>\u7ec4\u5408\u624b\u52bf\u7684\u624b\u6bb5<\/strong><\/p>\n\n\n\n<p>SwiftUI \u63d0\u4f9b\u4e86\u51e0\u4e2a\u7528\u4e8e\u624b\u52bf\u7684\u7ec4\u5408\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u624b\u52bf\u8fde\u63a5\u8d77\u6765\uff0c\u91cd\u6784\u6210\u5176\u4ed6\u7528\u9014\u7684\u624b\u52bf\u3002<\/p>\n\n\n\n<ul><li>simltaneously\uff08\u540c\u65f6\u8bc6\u522b\uff09<\/li><\/ul>\n\n\n\n<p>\u5c06\u4e00\u4e2a\u624b\u52bf\u4e0e\u53e6\u4e00\u4e2a\u624b\u52bf\u76f8\u7ed3\u5408\uff0c\u521b\u5efa\u4e00\u4e2a\u540c\u65f6\u8bc6\u522b\u4e24\u4e2a\u624b\u52bf\u7684\u65b0\u624b\u52bf\u3002\u4f8b\u5982\u5c06\u7f29\u653e\u624b\u52bf\u4e0e\u65cb\u8f6c\u624b\u52bf\u7ec4\u5408\uff0c\u5b9e\u73b0\u540c\u65f6\u5bf9\u56fe\u7247\u8fdb\u884c\u7f29\u653e\u548c\u65cb\u8f6c\u3002<\/p>\n\n\n\n<ul><li>sequenced\uff08\u5e8f\u5217\u8bc6\u522b\uff09<\/li><\/ul>\n\n\n\n<p>\u5c06\u4e24\u4e2a\u624b\u52bf\u8fde\u63a5\u8d77\u6765\uff0c\u53ea\u6709\u5728\u7b2c\u4e00\u4e2a\u624b\u52bf\u6210\u529f\u540e\uff0c\u624d\u4f1a\u6267\u884c\u7b2c\u4e8c\u4e2a\u624b\u52bf\u3002\u8b6c\u5982\uff0c\u5c06\u957f\u6309\u548c\u62d6\u62fd\u8fde\u63a5\u8d77\u6765\uff0c\u5b9e\u73b0\u53ea\u6709\u5f53\u6309\u538b\u6ee1\u8db3\u4e00\u5b9a\u65f6\u95f4\u540e\u624d\u5141\u8bb8\u62d6\u62fd\u3002<\/p>\n\n\n\n<ul><li>exclusively\uff08\u6392\u4ed6\u6027\u8bc6\u522b\uff09<\/li><\/ul>\n\n\n\n<p>\u5408\u5e76\u4e24\u4e2a\u624b\u52bf\uff0c\u4f46\u53ea\u6709\u5176\u4e2d\u4e00\u79cd\u624b\u52bf\u53ef\u4ee5\u88ab\u8bc6\u522b\u3002\u7cfb\u7edf\u4f1a\u4f18\u5148\u8003\u8651\u7b2c\u4e00\u4e2a\u624b\u52bf\u3002<\/p>\n\n\n\n<p>\u7ec4\u5408\u540e\u7684\u624b\u52bf\uff0cValue \u7c7b\u578b\u4e5f\u5c06\u53d1\u751f\u53d8\u5316\u3002\u4ecd\u53ef\u4f7f\u7528 <code>map<\/code> \u5c06\u5176\u8f6c\u6362\u6210\u66f4\u52a0\u6613\u7528\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n\n\n\n<p><strong>\u624b\u52bf\u7684\u5b9a\u4e49\u5f62\u5f0f<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">truct GestureDemo: View {\n    @GestureState(resetTransaction: .init(animation: .easeInOut)) var gestureValue = RotateAndMagnify()\n\n    var body: some View {\n        let rotateAndMagnifyGesture = MagnificationGesture()\n            .simultaneously(with: RotationGesture())\n            .updating($gestureValue) { value, state, _ in\n                state.angle = value.second ?? .zero\n                state.scale = value.first ?? 0\n            }\n\n        return Rectangle()\n            .fill(LinearGradient(colors: [.blue, .green, .pink], startPoint: .top, endPoint: .bottom))\n            .frame(width: 100, height: 100)\n            .shadow(radius: 8)\n            .rotationEffect(gestureValue.angle)\n            .scaleEffect(gestureValue.scale)\n            .gesture(rotateAndMagnifyGesture)\n    }\n\n    struct RotateAndMagnify {\n        var scale: CGFloat = 1.0\n        var angle: Angle = .zero\n    }\n}\n\n<\/pre>\n\n\n\n<p>\u53e6\u5916\uff0c\u4e5f\u53ef\u4ee5\u5c06\u624b\u52bf\u521b\u5efa\u6210\u7b26\u5408 Gesture \u534f\u8bae\u7684\u7ed3\u6784\u4f53\uff0c\u5982\u6b64\u5b9a\u4e49\u7684\u624b\u52bf\uff0c\u975e\u5e38\u9002\u5408\u88ab\u53cd\u590d\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u5c06\u624b\u52bf\u6216\u624b\u52bf\u5904\u7406\u903b\u8f91\u5c01\u88c5\u6210\u89c6\u56fe\u6269\u5c55\u53ef\u8fdb\u4e00\u6b65\u7b80\u5316\u4f7f\u7528\u96be\u5ea6\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u4e3a\u4e86\u7a81\u663e\u67d0\u4e9b\u65b9\u9762\u7684\u529f\u80fd\uff0c\u4e0b\u6587\u4e2d\u63d0\u4f9b\u7684\u6f14\u793a\u4ee3\u7801\u6216\u8bb8\u770b\u8d77\u6765\u6bd4\u8f83\u7e41\u7410\u3002\u5b9e\u9645\u4f7f\u7528\u65f6\uff0c\u53ef\u81ea\u884c\u7b80\u5316\u3002<\/p><\/blockquote>\n\n\n\n<h5 class=\"wp-block-heading\">\u793a\u4f8b\u4e00\uff1a\u8f7b\u626b<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p id=\"1.1_\u76ee\u6807\"><strong>1.1 \u76ee\u6807<\/strong><\/p>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u8f7b\u626b\uff08Swipe\uff09\u624b\u52bf\uff0c\u7740\u91cd\u6f14\u793a\u5982\u4f55\u521b\u5efa\u7b26\u5408 Gesture \u534f\u8bae\u7684\u7ed3\u6784\u4f53\uff0c\u5e76\u5bf9\u624b\u52bf\u6570\u636e\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n\n\n\n<p id=\"1.2_\u601d\u8def\"><strong>1.2 \u601d\u8def<\/strong><\/p>\n\n\n\n<p>\u5728 SwiftUI \u9884\u7f6e\u624b\u52bf\u4e2d\uff0c\u4ec5\u6709 DragGesture \u63d0\u4f9b\u4e86\u53ef\u7528\u4e8e\u5224\u65ad\u79fb\u52a8\u65b9\u5411\u7684\u6570\u636e\u3002\u6839\u636e\u504f\u79fb\u91cf\u6765\u786e\u5b9a\u8f7b\u626b\u65b9\u5411\uff0c\u4f7f\u7528 map \u5c06\u7e41\u6742\u7684\u6570\u636e\u8f6c\u6362\u6210\u7b80\u5355\u7684\u65b9\u5411\u6570\u636e\u3002<\/p>\n\n\n\n<p id=\"1.3_\u5b9e\u73b0\"><strong>1.3 \u5b9e\u73b0<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public struct SwipeGesture: Gesture {\n    public enum Direction: String {\n        case left, right, up, down\n    }\n\n    public typealias Value = Direction\n\n    private let minimumDistance: CGFloat\n    private let coordinateSpace: CoordinateSpace\n\n    public init(minimumDistance: CGFloat = 10, coordinateSpace: CoordinateSpace = .local) {\n        self.minimumDistance = minimumDistance\n        self.coordinateSpace = coordinateSpace\n    }\n\n    public var body: AnyGesture&lt;Value> {\n        AnyGesture(\n            DragGesture(minimumDistance: minimumDistance, coordinateSpace: coordinateSpace)\n                .map { value in\n                    let horizontalAmount = value.translation.width\n                    let verticalAmount = value.translation.height\n\n                    if abs(horizontalAmount) > abs(verticalAmount) {\n                        if horizontalAmount &lt; 0 { return .left } else { return .right }\n                    } else {\n                        if verticalAmount &lt; 0 { return .up } else { return .down }\n                    }\n                }\n        )\n    }\n}\n\npublic extension View {\n    func onSwipe(minimumDistance: CGFloat = 10,\n                 coordinateSpace: CoordinateSpace = .local,\n                 perform: @escaping (SwipeGesture.Direction) -> Void) -> some View {\n        gesture(\n            SwipeGesture(minimumDistance: minimumDistance, coordinateSpace: coordinateSpace)\n                .onEnded(perform)\n        )\n    }\n}<\/pre>\n\n\n\n<p><strong>1.4 \u6f14\u793a<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct SwipeTestView: View {\n    @State var direction = \"\"\n    var body: some View {\n        Rectangle()\n            .fill(.blue)\n            .frame(width: 200, height: 200)\n            .overlay(Text(direction))\n            .onSwipe { direction in\n                self.direction = direction.rawValue\n            }\n    }\n}<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/01\/swipeGestureDemo2-1.gif\" alt=\"\" class=\"wp-image-4943\" width=\"480\" height=\"363\"\/><\/figure><\/div>\n\n\n\n<p id=\"1.5_\u8bf4\u660e\"><strong>1.5 \u8bf4\u660e<\/strong><\/p>\n\n\n\n<ul><li>\u4e3a\u4ec0\u4e48\u4f7f\u7528 AnyGesture<\/li><\/ul>\n\n\n\n<p>\u5728 Gesture \u534f\u8bae\u4e2d\uff0c\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u9690\u85cf\u7684\u7c7b\u578b\u65b9\u6cd5\uff1a<code>_makeGesture<\/code>\u3002\u82f9\u679c\u76ee\u524d\u5e76\u6ca1\u6709\u63d0\u4f9b\u5e94\u8be5\u5982\u4f55\u5b9e\u73b0\u5b83\u7684\u6587\u6863\uff0c\u597d\u5728 SwiftUI \u63d0\u4f9b\u4e86\u4e00\u4e2a\u542b\u6709\u7ea6\u675f\u7684\u9ed8\u8ba4\u5b9e\u73b0\u3002\u5f53\u6211\u4eec\u4e0d\u5728\u7ed3\u6784\u4f53\u4e2d\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684 Value \u7c7b\u578b\u65f6\uff0cSwiftUI \u53ef\u4ee5\u63a8\u65ad\u51fa <code>Self.Body.Value<\/code>\uff0c\u6b64\u65f6\u53ef\u4ee5\u5c06 body \u58f0\u660e\u4e3a<code>some Gesture<\/code>\u3002\u4f46\u7531\u4e8e\u672c\u4f8b\u4e2d\u4f7f\u7528\u4e86\u81ea\u5b9a\u4e49 Value \u7c7b\u578b\uff0c\u56e0\u6b64\u5fc5\u987b\u5c06 body \u58f0\u660e\u4e3a<code>AnyGesture&lt;Value><\/code>\uff0c\u65b9\u53ef\u6ee1\u8db3\u542f\u7528<code>_makeGesture<\/code>\u9ed8\u8ba4\u5b9e\u73b0\u7684\u6761\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">  extension Gesture where Self.Value == Self.Body.Value {\n    public static func _makeGesture(gesture: SwiftUI._GraphValue&lt;Self>, inputs: SwiftUI._GestureInputs) -> SwiftUI._GestureOutputs&lt;Self.Body.Value>\n  }<\/pre>\n\n\n\n<p id=\"1.6_\u4e0d\u8db3\u4e0e\u6539\u5584\u65b9\u6cd5\"><strong>1.6 \u4e0d\u8db3\u4e0e\u6539\u5584\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<p>\u672c\u4f8b\u4e2d\u5e76\u6ca1\u6709\u5bf9\u624b\u52bf\u7684\u6301\u7eed\u65f6\u95f4\u3001\u79fb\u52a8\u901f\u5ea6\u7b49\u56e0\u7d20\u8fdb\u884c\u7efc\u5408\u8003\u91cf\uff0c\u5f53\u524d\u7684\u5b9e\u73b0\u4e25\u683c\u610f\u4e49\u4e0a\u5e76\u4e0d\u80fd\u7b97\u662f\u771f\u6b63\u8f7b\u626b\u3002\u5982\u679c\u60f3\u5b9e\u73b0\u4e25\u683c\u610f\u4e49\u4e0a\u7684\u8f7b\u626b\u53ef\u4ee5\u91c7\u7528\u5982\u4e0b\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul><li>\u6539\u6210\u793a\u4f8b 2 \u7684\u65b9\u5f0f\uff0c\u7528 ViewModifier \u6765\u5305\u88c5 DragGesture<\/li><li>\u7528 State \u8bb0\u5f55\u6ed1\u52a8\u65f6\u95f4<\/li><li>\u5728 onEnded \u4e2d\uff0c\u53ea\u6709\u6ee1\u8db3\u901f\u5ea6\u3001\u8ddd\u79bb\u3001\u504f\u5dee\u7b49\u8981\u6c42\u7684\u60c5\u51b5\u4e0b\uff0c\u624d\u56de\u8c03\u7528\u6237\u7684\u95ed\u5305\uff0c\u5e76\u4f20\u9012\u65b9\u5411<\/li><\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">\u793a\u4f8b\u4e8c\uff1a\u8ba1\u65f6\u6309\u538b<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p id=\"2.1_\u76ee\u6807\"><strong>2.1 \u76ee\u6807<\/strong><\/p>\n\n\n\n<p>\u5b9e\u73b0\u4e00\u4e2a\u53ef\u4ee5\u8bb0\u5f55\u65f6\u957f\u7684\u6309\u538b\u624b\u52bf\u3002\u624b\u52bf\u5728\u6309\u538b\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u8fdb\u884c\u7c7b\u4f3c onChanged \u7684\u56de\u8c03\u3002\u672c\u4f8b\u7a0b\u7740\u91cd\u6f14\u793a\u5982\u4f55\u901a\u8fc7\u89c6\u56fe\u4fee\u9970\u5668\u5305\u88c5\u624b\u52bf\u7684\u65b9\u6cd5\u4ee5\u53ca GestureState \u7684\u4f7f\u7528\u3002<\/p>\n\n\n\n<p id=\"2.2_\u601d\u8def\"><strong>2.2 \u601d\u8def<\/strong><\/p>\n\n\n\n<p>\u901a\u8fc7\u8ba1\u65f6\u5668\u5728\u6307\u5b9a\u65f6\u95f4\u95f4\u9694\u540e\u5411\u95ed\u5305\u4f20\u9012\u5f53\u524d\u6309\u538b\u7684\u6301\u7eed\u65f6\u95f4\u3002\u4f7f\u7528 GestureState \u4fdd\u5b58\u70b9\u51fb\u5f00\u59cb\u7684\u65f6\u95f4\uff0c\u6309\u538b\u7ed3\u675f\u540e\uff0c\u4e0a\u6b21\u6309\u538b\u7684\u8d77\u59cb\u65f6\u95f4\u4f1a\u88ab\u624b\u52bf\u81ea\u52a8\u6e05\u9664\u3002<\/p>\n\n\n\n<p id=\"2.3_\u5b9e\u73b0\"><strong>2.3 \u5b9e\u73b0<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public struct PressGestureViewModifier: ViewModifier {\n    @GestureState private var startTimestamp: Date?\n    @State private var timePublisher: Publishers.Autoconnect&lt;Timer.TimerPublisher>\n    private var onPressing: (TimeInterval) -> Void\n    private var onEnded: () -> Void\n\n    public init(interval: TimeInterval = 0.016, onPressing: @escaping (TimeInterval) -> Void, onEnded: @escaping () -> Void) {\n        _timePublisher = State(wrappedValue: Timer.publish(every: interval, tolerance: nil, on: .current, in: .common).autoconnect())\n        self.onPressing = onPressing\n        self.onEnded = onEnded\n    }\n\n    public func body(content: Content) -> some View {\n        content\n            .gesture(\n                DragGesture(minimumDistance: 0, coordinateSpace: .local)\n                    .updating($startTimestamp, body: { _, current, _ in\n                        if current == nil {\n                            current = Date()\n                        }\n                    })\n                    .onEnded { _ in\n                        onEnded()\n                    }\n            )\n            .onReceive(timePublisher, perform: { timer in\n                if let startTimestamp = startTimestamp {\n                    let duration = timer.timeIntervalSince(startTimestamp)\n                    onPressing(duration)\n                }\n            })\n    }\n}\n\npublic extension View {\n    func onPress(interval: TimeInterval = 0.016, onPressing: @escaping (TimeInterval) -> Void, onEnded: @escaping () -> Void) -> some View {\n        modifier(PressGestureViewModifier(interval: interval, onPressing: onPressing, onEnded: onEnded))\n    }\n}<\/pre>\n\n\n\n<p><strong>2.4 \u6f14\u793a<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct PressGestureView: View {\n    @State var scale: CGFloat = 1\n    @State var duration: TimeInterval = 0\n    var body: some View {\n        VStack {\n            Circle()\n                .fill(scale == 1 ? .blue : .orange)\n                .frame(width: 50, height: 50)\n                .scaleEffect(scale)\n                .overlay(Text(duration, format: .number.precision(.fractionLength(1))))\n                .onPress { duration in\n                    self.duration = duration\n                    scale = 1 + duration * 2\n                } onEnded: {\n                    if duration > 1 {\n                        withAnimation(.easeInOut(duration: 2)) {\n                            scale = 1\n                        }\n                    } else {\n                        withAnimation(.easeInOut) {\n                            scale = 1\n                        }\n                    }\n                    duration = 0\n                }\n        }\n    }\n}\n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/01\/pressGestureDemo.2022-01-08-13_50_59.gif\" alt=\"\" class=\"wp-image-4945\" width=\"519\" height=\"362\"\/><\/figure><\/div>\n\n\n\n<p id=\"2.5_\u8bf4\u660e\"><strong>2.5 \u8bf4\u660e<\/strong><\/p>\n\n\n\n<ul><li>GestureState \u6570\u636e\u7684\u590d\u539f\u65f6\u95f4\u5728 onEnded \u4e4b\u524d\uff0c\u5728 onEnded \u4e2d\uff0cstartTimestamp \u5df2\u7ecf\u6062\u590d\u4e3a nil<\/li><li>DragGesture \u4ecd\u662f\u6700\u597d\u7684\u5b9e\u73b0\u8f7d\u4f53\u3002TapGesture\u3001LongPressGesture \u5747\u5728\u6ee1\u8db3\u89e6\u53d1\u6761\u4ef6\u540e\u4f1a\u81ea\u52a8\u7ec8\u6b62\u624b\u52bf\uff0c\u65e0\u6cd5\u5b9e\u73b0\u5bf9\u4efb\u610f\u65f6\u957f\u7684\u652f\u6301<\/li><\/ul>\n\n\n\n<p id=\"2.6_\u4e0d\u8db3\u53ca\u6539\u5584\u65b9\u6cd5\"><strong>2.6 \u4e0d\u8db3\u53ca\u6539\u5584\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<p>\u5f53\u524d\u7684\u89e3\u51b3\u65b9\u6848\u6ca1\u6709\u63d0\u4f9b\u7c7b\u4f3c LongPressGesture \u6309\u538b\u4e2d\u4f4d\u7f6e\u504f\u79fb\u9650\u5b9a\u8bbe\u7f6e\uff0c\u53e6\u5916\u5c1a\u672a\u5728 onEnded \u4e2d\u63d0\u4f9b\u672c\u6b21\u6309\u538b\u7684\u603b\u6301\u7eed\u65f6\u957f\u3002<\/p>\n\n\n\n<ul><li>\u5728 updating \u4e2d\u5bf9\u504f\u79fb\u91cf\u8fdb\u884c\u5224\u65ad\uff0c\u5982\u679c\u6309\u538b\u70b9\u7684\u504f\u79fb\u8d85\u51fa\u4e86\u6307\u5b9a\u7684\u8303\u56f4\uff0c\u5219\u4e2d\u65ad\u8ba1\u65f6\u3002\u5e76\u5728 updating \u4e2d\uff0c\u8c03\u7528\u7528\u6237\u63d0\u4f9b\u7684 onEnded \u95ed\u5305\uff0c\u5e76\u8fdb\u884c\u6807\u8bb0<\/li><li>\u5728\u624b\u52bf\u7684 onEnded \u4e2d\uff0c\u5982\u679c\u7528\u6237\u63d0\u4f9b\u7684 onEnded \u95ed\u5305\u5df2\u7ecf\u88ab\u8c03\u7528\uff0c\u5219\u4e0d\u4f1a\u518d\u6b64\u8c03\u7528<\/li><li>\u4f7f\u7528 State \u66ff\u6362 GestureState\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u624b\u52bf\u7684 onEnded \u4e2d\u63d0\u4f9b\u603b\u6301\u7eed\u65f6\u95f4\u3002\u9700\u81ea\u884c\u7f16\u5199 State \u7684\u6570\u636e\u6062\u590d\u4ee3\u7801<\/li><li>\u7531\u4e8e\u4f7f\u7528\u4e86 State \u66ff\u6362 GestureState\uff0c\u903b\u8f91\u5224\u65ad\u5c31\u53ef\u4ee5\u4ece updating \u79fb\u52a8\u5230 onChanged \u4e2d<\/li><\/ul>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"\u793a\u4f8b\u4e09\uff1a\u9644\u5e26\u4f4d\u7f6e\u4fe1\u606f\u7684\u70b9\u51fb\">\u793a\u4f8b\u4e09\uff1a\u9644\u5e26\u4f4d\u7f6e\u4fe1\u606f\u7684\u70b9\u51fb<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p id=\"3.1_\u76ee\u6807\"><strong>3.1 \u76ee\u6807<\/strong><\/p>\n\n\n\n<p>\u5b9e\u73b0\u63d0\u4f9b\u89e6\u6478\u4f4d\u7f6e\u4fe1\u606f\u7684\u70b9\u51fb\u624b\u52bf\uff08\u652f\u6301\u70b9\u51fb\u6b21\u6570\u8bbe\u5b9a\uff09\u3002\u672c\u4f8b\u4e3b\u8981\u6f14\u793a simultaneously \u7684\u7528\u6cd5\u4ee5\u53ca\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u56de\u8c03\u65f6\u95f4\u70b9\uff08onEnded\uff09\u3002<\/p>\n\n\n\n<p id=\"3.2_\u601d\u8def\"><strong>3.2 \u601d\u8def<\/strong><\/p>\n\n\n\n<p>\u624b\u52bf\u7684\u54cd\u5e94\u611f\u89c9\u5e94\u4e0e TapGesture \u5b8c\u5168\u4e00\u81f4\u3002\u4f7f\u7528 simultaneously \u5c06\u4e24\u79cd\u624b\u52bf\u8054\u5408\u8d77\u6765\uff0c\u4ece DrageGesture \u4e2d\u83b7\u53d6\u4f4d\u7f6e\u6570\u636e\uff0c\u4ece TapGesture \u4e2d\u9000\u51fa\u3002<\/p>\n\n\n\n<p id=\"3.3_\u5b9e\u73b0\"><strong>3.3 \u5b9e\u73b0<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public struct TapWithLocation: ViewModifier {\n    @State private var locations: CGPoint?\n    private let count: Int\n    private let coordinateSpace: CoordinateSpace\n    private var perform: (CGPoint) -> Void\n\n    init(count: Int = 1, coordinateSpace: CoordinateSpace = .local, perform: @escaping (CGPoint) -> Void) {\n        self.count = count\n        self.coordinateSpace = coordinateSpace\n        self.perform = perform\n    }\n\n    public func body(content: Content) -> some View {\n        content\n            .gesture(\n                DragGesture(minimumDistance: 0, coordinateSpace: coordinateSpace)\n                    .onChanged { value in\n                        locations = value.location\n                    }\n                    .simultaneously(with:\n                        TapGesture(count: count)\n                            .onEnded {\n                                perform(locations ?? .zero)\n                                locations = nil\n                            }\n                    )\n            )\n    }\n}\n\npublic extension View {\n    func onTapGesture(count: Int = 1, coordinateSpace: CoordinateSpace = .local, perform: @escaping (CGPoint) -> Void) -> some View {\n        modifier(TapWithLocation(count: count, coordinateSpace: coordinateSpace, perform: perform))\n    }\n}<\/pre>\n\n\n\n<p><strong>3.4 \u6f14\u793a<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct TapWithLocationView: View {\n    @State var unitPoint: UnitPoint = .center\n    var body: some View {\n        Rectangle()\n            .fill(RadialGradient(colors: [.yellow, .orange, .red, .pink], center: unitPoint, startRadius: 10, endRadius: 170))\n            .frame(width: 300, height: 300)\n            .onTapGesture(count:2) { point in\n                withAnimation(.easeInOut) {\n                    unitPoint = UnitPoint(x: point.x \/ 300, y: point.y \/ 300)\n                }\n            }\n    }\n}<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/01\/TapWithLocationDemo.gif\" alt=\"\" class=\"wp-image-4946\" width=\"457\" height=\"346\"\/><\/figure><\/div>\n\n\n\n<p id=\"3.5_\u8bf4\u660e\"><strong>3.5 \u8bf4\u660e<\/strong><\/p>\n\n\n\n<ul><li>\u5f53 DragGesture \u7684 minimumDistance \u8bbe\u7f6e\u4e3a 0 \u65f6\uff0c\u5176\u7b2c\u4e00\u6761\u6570\u636e\u7684\u4ea7\u751f\u65f6\u95f4\u4e00\u5b9a\u65e9\u4e8e TapGesture(count:1) \u7684\u6fc0\u6d3b\u65f6\u95f4<\/li><li>\u5728 simultaneously \u4e2d\uff0c\u4e00\u5171\u6709\u4e09\u4e2a onEndend \u65f6\u673a\u3002\u624b\u52bf 1 \u7684 onEnded\uff0c\u624b\u52bf 2 \u7684 onEnded\uff0c\u4ee5\u53ca\u5408\u5e76\u540e\u624b\u52bf\u7684 onEnded\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u9009\u62e9\u5728 TapGesture \u7684 onEnded \u4e2d\u56de\u8c03\u7528\u6237\u7684\u95ed\u5305<\/li><\/ul>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"\u603b\u7ed3\">\u603b\u7ed3<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5f53\u524d SwiftUI \u7684\u624b\u52bf\uff0c\u6682\u5904\u4e8e\u4f7f\u7528\u95e8\u69db\u4f4e\u4f46\u80fd\u529b\u4e0a\u9650\u4e0d\u8db3\u7684\u72b6\u51b5\uff0c\u4ec5\u4f7f\u7528 SwiftUI \u7684\u539f\u751f\u624b\u6bb5\u65e0\u6cd5\u5b9e\u73b0\u975e\u5e38\u590d\u6742\u7684\u624b\u52bf\u903b\u8f91\u3002\u5c06\u6765\u627e\u65f6\u95f4\u6211\u4eec\u518d\u901a\u8fc7\u5176\u5b83\u7684\u6587\u7ae0\u6765\u7814\u7a76\u6709\u5173\u624b\u52bf\u4e4b\u95f4\u7684\u4f18\u5148\u7ea7\u3001\u4f7f\u7528 GestureMask \u9009\u62e9\u6027\u5931\u6548\uff0c\u4ee5\u53ca\u5982\u4f55\u540c UIGestureRecognizer \u5408\u4f5c\u521b\u5efa\u590d\u6742\u624b\u52bf\u7b49\u8bae\u9898\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1ahttps:\/\/www.fatbobman.com\/posts\/swiftuiGesture\/ \u4e0d\u540c\u4e8e\u4f17 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4938"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4938"}],"version-history":[{"count":5,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4938\/revisions"}],"predecessor-version":[{"id":4948,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4938\/revisions\/4948"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}