{"id":3947,"date":"2025-05-30T14:03:21","date_gmt":"2025-06-04T06:04:20","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3947"},"modified":"2025-06-04T14:03:21","modified_gmt":"2025-06-04T06:04:20","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%89%8b%e5%8a%bf%e8%ad%98%e5%88%a5-2","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%89%8b%e5%8a%bf%e8%ad%98%e5%88%a5-2\/","title":{"rendered":"\u638c\u63e1Swift\u624b\u52e2\u8b58\u5225\uff1a\u5f9e\u57fa\u790e\u5230\u81ea\u5b9a\u7fa9\u7684\u5b8c\u6574\u6307\u5357"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, UIGestureRecognizer, \u624b\u52bf\u8bc6\u522b, iOS\u5f00\u53d1, \u81ea\u5b9a\u4e49\u624b\u52bf\u8bc6\u522b\u5668, Swift\u6559\u7a0b\"><\/p>\n<h1>\u638c\u63e1Swift\u624b\u52e2\u8b58\u5225\uff1a\u5f9e\u57fa\u790e\u5230\u81ea\u5b9a\u7fa9\u7684\u5b8c\u6574\u6307\u5357<\/h1>\n<p>Swift\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bciOS\u548cmacOS\u7684\u61c9\u7528\u7a0b\u5e8f\u958b\u767c\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7a2e\u76f4\u89c0\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u4f86\u8655\u7406\u624b\u52e2\u8b58\u5225\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u5275\u5efa\u66f4\u5177\u4e92\u52d5\u6027\u7684\u61c9\u7528\u9ad4\u9a57\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u5728Swift\u4e2d\u4f7f\u7528\u624b\u52e2\u8b58\u5225\uff0c\u5305\u62ecUIGestureRecognizer\u985e\u7684\u7528\u6cd5\u3001\u70ba\u8996\u5716\u6dfb\u52a0\u624b\u52e2\u8b58\u5225\u5668\uff0c\u4ee5\u53ca\u5982\u4f55\u81ea\u5b9a\u7fa9\u624b\u52e2\u8b58\u5225\u5668\u3002<\/p>\n<h2>\u4f7f\u7528UIGestureRecognizer\u985e\u5be6\u73fe\u624b\u52e2\u8b58\u5225<\/h2>\n<p>UIGestureRecognizer\u662f\u4e00\u500b\u62bd\u8c61\u985e\uff0c\u70ba\u624b\u52e2\u8b58\u5225\u63d0\u4f9b\u4e86\u7d71\u4e00\u7684\u63a5\u53e3\u3002\u5b83\u7c21\u5316\u4e86\u624b\u52e2\u8655\u7406\u7684\u904e\u7a0b\uff0c\u4f7f\u958b\u767c\u8005\u66f4\u5bb9\u6613\u69cb\u5efa\u4e92\u52d5\u6027\u5f37\u7684\u61c9\u7528\u3002UIGestureRecognizer\u985e\u63d0\u4f9b\u4e86\u591a\u500b\u65b9\u6cd5\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u9748\u6d3b\u8655\u7406\u624b\u52e2\u8b58\u5225\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<ul>\n<li><strong>addTarget:action:<\/strong> &#8211; \u7576\u624b\u52e2\u88ab\u8b58\u5225\u6642\uff0c\u5c07\u76ee\u6a19\u548c\u52d5\u4f5c\u95dc\u806f\u8d77\u4f86\u3002<\/li>\n<li><strong>removeTarget:action:<\/strong> &#8211; \u79fb\u9664\u4e0d\u518d\u9700\u8981\u7684\u624b\u52e2\u8b58\u5225\u76ee\u6a19\u3002<\/li>\n<li><strong>requireGestureRecognizerToFail:<\/strong> &#8211; \u8a2d\u7f6e\u4e00\u500b\u624b\u52e2\u8b58\u5225\u5668\uff0c\u5fc5\u9808\u5148\u5931\u6557\u624d\u80fd\u89f8\u767c\u53e6\u4e00\u500b\u624b\u52e2\u8b58\u5225\u5668\u3002<\/li>\n<li><strong>locationInView:<\/strong> &#8211; \u8fd4\u56de\u624b\u52e2\u5728\u6307\u5b9a\u8996\u5716\u4e2d\u7684\u4f4d\u7f6e\u3002<\/li>\n<li><strong>state:<\/strong> &#8211; \u6aa2\u67e5\u624b\u52e2\u8b58\u5225\u5668\u7684\u7576\u524d\u72c0\u614b\u3002<\/li>\n<\/ul>\n<h2>\u70ba\u8996\u5716\u6dfb\u52a0\u624b\u52e2\u8b58\u5225\u5668<\/h2>\n<p>\u8981\u70ba\u8996\u5716\u6dfb\u52a0\u624b\u52e2\u8b58\u5225\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528UIView\u985e\u7684<strong>addGestureRecognizer:<\/strong>\u65b9\u6cd5\uff0c\u9019\u500b\u65b9\u6cd5\u63a5\u53d7\u4e00\u500bUIGestureRecognizer\u5c0d\u8c61\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728Swift\u4e2d\u5be6\u73fe\u7684\u4e00\u500b\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"brush: python\">\nlet tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))\nview.addGestureRecognizer(tapGestureRecognizer)\n<\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u78bc\u5275\u5efa\u4e86\u4e00\u500bUITapGestureRecognizer\u5c0d\u8c61\u4e26\u5c07\u5176\u6dfb\u52a0\u81f3\u8996\u5716\u4e2d\u3002\u7576\u7528\u6236\u9032\u884c\u9ede\u64ca\u624b\u52e2\u6642\uff0c\u5c07\u6703\u89f8\u767c<strong>handleTap<\/strong>\u65b9\u6cd5\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5c0d\u89f8\u767c\u4e8b\u4ef6\u4f5c\u51fa\u53cd\u61c9\u3002<\/p>\n<h2>\u81ea\u5b9a\u7fa9\u624b\u52e2\u8b58\u5225\u5668<\/h2>\n<p>Swift\u5141\u8a31\u958b\u767c\u8005\u901a\u904e\u7e7c\u627fUIGestureRecognizer\u985e\u4f86\u81ea\u5b9a\u7fa9\u624b\u52e2\u8b58\u5225\u5668\u3002\u9019\u6a23\u53ef\u4ee5\u6eff\u8db3\u7279\u5b9a\u7684\u61c9\u7528\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u81ea\u5b9a\u7fa9\u624b\u52e2\u8b58\u5225\u5668\u7684\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\nclass MyGestureRecognizer: UIGestureRecognizer {\n    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {\n        \/\/ \u8655\u7406\u89f8\u6478\u4e8b\u4ef6\n        state = .began \/\/ \u8a2d\u5b9a\u624b\u52e2\u72c0\u614b\n    }\n}\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70baMyGestureRecognizer\u7684\u624b\u52e2\u8b58\u5225\u5668\uff0c\u4e26\u5728\u89f8\u6478\u958b\u59cb\u6642\u66f4\u65b0\u624b\u52e2\u72c0\u614b\u3002\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u6dfb\u52a0\u66f4\u591a\u7684\u908f\u8f2f\u4f86\u8655\u7406\u4e0d\u540c\u7684\u624b\u52e2\u72c0\u614b\u3002<\/p>\n<h2>\u7d50\u8a9e<\/h2>\n<p>\u672c\u6587\u6df1\u5165\u4ecb\u7d39\u4e86\u5982\u4f55\u5728Swift\u4e2d\u9032\u884c\u624b\u52e2\u8b58\u5225\uff0c\u5305\u62ec\u4f7f\u7528UIGestureRecognizer\u985e\u7684\u57fa\u672c\u65b9\u6cd5\u3001\u5982\u4f55\u70ba\u8996\u5716\u6dfb\u52a0\u624b\u52e2\u8b58\u5225\u5668\uff0c\u4ee5\u53ca\u5982\u4f55\u81ea\u5b9a\u7fa9\u624b\u52e2\u8b58\u5225\u5668\u3002\u638c\u63e1\u9019\u4e9b\u6280\u8853\u5f8c\uff0c\u958b\u767c\u8005\u5c07\u80fd\u5920\u5275\u5efa\u51fa\u66f4\u52a0\u4e92\u52d5\u548c\u53cb\u597d\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\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\u624b\u52e2\u8b58\u5225\uff0c\u5305\u62ec\u5982\u4f55\u4f7f\u7528UIGestureRecognizer\u985e\u4f86\u5be6\u73fe\u624b\u52e2\u8b58\u5225\u3001\u5982\u4f55\u70ba\u8996\u5716\u6dfb\u52a0\u624b\u52e2\u8b58\u5225\u5668\u3001\u5982\u4f55\u81ea\u5b9a\u7fa9\u624b\u52e2\u8b58\u5225\u5668\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-3947","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-11F","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3947","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=3947"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3947\/revisions"}],"predecessor-version":[{"id":3948,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3947\/revisions\/3948"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}