{"id":3963,"date":"2025-05-31T11:39:16","date_gmt":"2025-06-05T03:50:15","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3963"},"modified":"2025-06-05T11:39:16","modified_gmt":"2025-06-05T03:50:15","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e6%8b%96%e5%8b%95%e6%89%8b%e5%8b%a2%e8%ad%98%e5%88%a5%e5%99%a8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e6%8b%96%e5%8b%95%e6%89%8b%e5%8b%a2%e8%ad%98%e5%88%a5%e5%99%a8\/","title":{"rendered":"2025 \u6700\u65b0 Swift \u62d6\u52d5\u624b\u52e2\u8b58\u5225\u5668\u6559\u5b78\uff1a\u4f7f\u7528 UIPanGestureRecognizer \u5be6\u73fe\u624b\u52e2\u97ff\u61c9"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, UIPanGestureRecognizer, \u62d6\u52a8\u624b\u52bf\u8bc6\u522b\u5668, \u4f4d\u79fb, \u901f\u5ea6, \u624b\u52bf\u8bc6\u522b, iOS \u5f00\u53d1\"><\/p>\n<h1>2025 \u6700\u65b0 Swift \u62d6\u52d5\u624b\u52e2\u8b58\u5225\u5668\u6559\u5b78\uff1a\u4f7f\u7528 UIPanGestureRecognizer \u5be6\u73fe\u624b\u52e2\u97ff\u61c9<\/h1>\n<p>\u57282025\u5e74\uff0c\u62d6\u52d5\u624b\u52e2\u8b58\u5225\u5668\u4f9d\u7136\u662f Swift \u958b\u767c\u4e2d\u6700\u5e38\u7528\u7684\u529f\u80fd\u4e4b\u4e00\u3002\u4f7f\u7528 `UIPanGestureRecognizer` \u985e\u53ef\u4ee5\u8f15\u9b06\u8b58\u5225\u7528\u6236\u7684\u62d6\u52d5\u624b\u52e2\uff0c\u4e26\u4e14\u80fd\u5920\u7372\u53d6\u624b\u52e2\u7684\u4f4d\u79fb\u548c\u901f\u5ea6\u7b49\u91cd\u8981\u4fe1\u606f\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 `UIPanGestureRecognizer`\uff0c\u4ee5\u53ca\u6700\u4f73\u5be6\u8e10\u548c\u5e38\u898b\u932f\u8aa4\u6392\u9664\u6280\u5de7\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 UIPanGestureRecognizer \u5be6\u73fe\u62d6\u52d5\u624b\u52e2\u8b58\u5225\u5668<\/h2>\n<p>\u8981\u5be6\u73fe\u62d6\u52d5\u624b\u52e2\u8b58\u5225\u5668\uff0c\u9996\u5148\u9700\u8981\u5275\u5efa\u4e00\u500b `UIPanGestureRecognizer` \u5c0d\u8c61\uff0c\u4e26\u5c07\u5176\u6dfb\u52a0\u5230\u8981\u8b58\u5225\u624b\u52e2\u7684\u8996\u5716\u4e0a\u3002\u4ee5\u4e0b\u662f\u5be6\u4f5c\u7684\u6b65\u9a5f\uff1a<\/p>\n<p>1. \u5275\u5efa `UIPanGestureRecognizer` \u5c0d\u8c61\u3002<br \/>\n2. \u5c07\u624b\u52e2\u8b58\u5225\u5668\u6dfb\u52a0\u5230\u8996\u5716\u3002<br \/>\n3. \u5be6\u4f5c\u8655\u7406\u624b\u52e2\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u7bc4\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture))<br \/>\nview.addGestureRecognizer(panGestureRecognizer)<br \/>\n&#8220;`<\/p>\n<p>\u7576\u7528\u6236\u5728\u8996\u5716\u4e0a\u9032\u884c\u62d6\u52d5\u6642\uff0c\u5c07\u89f8\u767c `handlePanGesture` \u65b9\u6cd5\uff1a<\/p>\n<p>&#8220;`swift<br \/>\n@objc func handlePanGesture(gesture: UIPanGestureRecognizer) {<br \/>\n    \/\/ \u8655\u7406\u62d6\u52d5\u624b\u52e2<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h2>\u7372\u53d6\u62d6\u52d5\u624b\u52e2\u7684\u4f4d\u79fb\u548c\u901f\u5ea6<\/h2>\n<p>\u5728\u8655\u7406\u62d6\u52d5\u624b\u52e2\u6642\uff0c\u53ef\u4ee5\u901a\u904e `UIPanGestureRecognizer` \u7684 `translation(in:)` \u65b9\u6cd5\u4f86\u7372\u53d6\u4f4d\u79fb\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet translation = gesture.translation(in: view)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u500b\u65b9\u6cd5\u8fd4\u56de\u4e00\u500b `CGPoint` \u985e\u578b\u7684\u503c\uff0c\u8868\u793a\u62d6\u52d5\u624b\u52e2\u7684\u4f4d\u79fb\u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u65b9\u5f0f\u8a08\u7b97\u62d6\u52d5\u7684\u8ddd\u96e2\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet distance = sqrt(translation.x * translation.x + translation.y * translation.y)<br \/>\n&#8220;`<\/p>\n<p>\u6b64\u5916\uff0c\u4f7f\u7528 `velocity(in:)` \u65b9\u6cd5\u53ef\u4ee5\u7372\u53d6\u62d6\u52d5\u7684\u901f\u5ea6\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet velocity = gesture.velocity(in: view)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u500b\u65b9\u6cd5\u4e5f\u8fd4\u56de\u4e00\u500b `CGPoint` \u985e\u578b\u7684\u503c\uff0c\u8868\u793a\u624b\u52e2\u7684\u901f\u5ea6\u3002\u8a08\u7b97\u901f\u5ea6\u7684\u4ee3\u78bc\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet speed = sqrt(velocity.x * velocity.x + velocity.y * velocity.y)<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u554f\u984c\uff1a<\/p>\n<p>&#8211; **\u624b\u52e2\u672a\u8b58\u5225**\uff1a\u78ba\u4fdd\u624b\u52e2\u8b58\u5225\u5668\u5df2\u6b63\u78ba\u6dfb\u52a0\u5230\u8996\u5716\u4e0a\uff0c\u4e26\u4e14\u6c92\u6709\u5176\u4ed6\u624b\u52e2\u8b58\u5225\u5668\u5728\u5e72\u64fe\u3002<br \/>\n&#8211; **\u901f\u5ea6\u548c\u4f4d\u79fb\u503c\u4e0d\u6b63\u78ba**\uff1a\u78ba\u8a8d\u4f7f\u7528\u7684\u8996\u5716\u53c3\u6578\u6b63\u78ba\uff0c\u4e26\u5728\u9069\u7576\u7684\u6642\u6a5f\u8abf\u7528\u4f4d\u79fb\u548c\u901f\u5ea6\u65b9\u6cd5\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u62d6\u52d5\u624b\u52e2\u4e4b\u5916\uff0c\u60a8\u53ef\u4ee5\u5c07\u6b64\u529f\u80fd\u64f4\u5c55\u5230\u4ee5\u4e0b\u61c9\u7528\uff1a<\/p>\n<p>&#8211; **\u62d6\u52d5\u8996\u5716**\uff1a\u5141\u8a31\u7528\u6236\u901a\u904e\u62d6\u52d5\u624b\u52e2\u79fb\u52d5\u8996\u5716\u3002<br \/>\n&#8211; **\u904a\u6232\u958b\u767c**\uff1a\u5be6\u73fe\u89d2\u8272\u6216\u7269\u9ad4\u7684\u62d6\u52d5\u548c\u79fb\u52d5\u3002<br \/>\n&#8211; **\u81ea\u5b9a\u7fa9\u624b\u52e2\u8b58\u5225**\uff1a\u7d50\u5408\u5176\u4ed6\u624b\u52e2\u8b58\u5225\u5668\uff0c\u4f8b\u5982\u7e2e\u653e\u6216\u65cb\u8f49\uff0c\u5275\u5efa\u66f4\u52a0\u8907\u96dc\u7684\u4ea4\u4e92\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 `UIPanGestureRecognizer` \u4f86\u5be6\u73fe\u62d6\u52d5\u624b\u52e2\u8b58\u5225\u5668\uff0c\u4e26\u77ad\u89e3\u5982\u4f55\u7372\u53d6\u4f4d\u79fb\u548c\u901f\u5ea6\u7b49\u91cd\u8981\u4fe1\u606f\u3002\u901a\u904e\u9019\u4e9b\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5be6\u73fe\u624b\u52e2\u8b58\u5225\u5668\uff0c\u589e\u5f37\u61c9\u7528\u7684\u7528\u6236\u4e92\u52d5\u9ad4\u9a57\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**\u554f\uff1a\u5982\u4f55\u9632\u6b62\u624b\u52e2\u8b58\u5225\u5668\u8207\u5176\u4ed6\u624b\u52e2\u885d\u7a81\uff1f**<br \/>\n\u7b54\uff1a\u60a8\u53ef\u4ee5\u70ba\u6bcf\u500b\u624b\u52e2\u8b58\u5225\u5668\u8a2d\u7f6e `delegate`\uff0c\u4e26\u5be6\u4f5c `gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)` \u65b9\u6cd5\uff0c\u4f86\u63a7\u5236\u540c\u6642\u8b58\u5225\u7684\u624b\u52e2\u3002<\/p>\n<p>**\u554f\uff1a\u5982\u4f55\u7372\u53d6\u591a\u9ede\u89f8\u63a7\u7684\u62d6\u52d5\u624b\u52e2\uff1f**<br \/>\n\u7b54\uff1a\u4f7f\u7528 `UIPanGestureRecognizer` \u7684 `numberOfTouches` \u5c6c\u6027\u4f86\u6aa2\u67e5\u89f8\u63a7\u6578\u91cf\uff0c\u7136\u5f8c\u6839\u64da\u9700\u8981\u8655\u7406\u591a\u9ede\u89f8\u63a7\u3002<\/p>\n<p>**\u554f\uff1a\u62d6\u52d5\u624b\u52e2\u7684\u79fb\u52d5\u7bc4\u570d\u5982\u4f55\u9650\u5236\uff1f**<br \/>\n\u7b54\uff1a\u5728 `handlePanGesture` \u65b9\u6cd5\u5167\uff0c\u901a\u904e\u6aa2\u67e5 `translation` \u503c\u53ef\u4ee5\u9650\u5236\u624b\u52e2\u7684\u79fb\u52d5\u7bc4\u570d\uff0c\u5f9e\u800c\u907f\u514d\u62d6\u52d5\u5230\u4e0d\u5e0c\u671b\u7684\u5340\u57df\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u62d6\u52d5\u624b\u52e2\u8b58\u5225\u5668\uff0c\u5305\u62ec\u5982\u4f55\u4f7f\u7528UIPanGestureRecognizer\u985e\u4f86\u5be6\u73fe\u62d6\u52d5\u624b\u52e2\u8b58\u5225\u5668\u3001\u5982\u4f55\u7372\u53d6\u62d6\u52d5\u624b\u52e2\u7684\u4f4d\u79fb\u548c\u901f\u5ea6\u7b49\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-3963","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-11V","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3963"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3963\/revisions"}],"predecessor-version":[{"id":3964,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3963\/revisions\/3964"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}