{"id":7418,"date":"2025-06-04T14:34:50","date_gmt":"2025-06-05T06:46:49","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7418"},"modified":"2025-06-05T14:34:50","modified_gmt":"2025-06-05T06:46:49","slug":"https-badgameshow-com-steven-380","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-380\/","title":{"rendered":"Swift \u89f8\u63a7\u4e8b\u4ef6\u8207\u624b\u52e2\u8fa8\u8b58\u5b8c\u5168\u6307\u5357 (UITapGestureRecognizer) &#8211; 2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UITapGestureRecognizer, \u624b\u52e2\u8fa8\u8b58, \u89f8\u63a7\u4e8b\u4ef6\"><\/p>\n<h2>\u5f15\u8a00<\/h2>\n<p>\u5728 iOS \u958b\u767c\u4e2d\uff0c<strong>UITapGestureRecognizer<\/strong> \u662f\u4e00\u500b\u6975\u70ba\u91cd\u8981\u7684\u5de5\u5177\uff0c\u5b83\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u7c21\u55ae\u5730\u70ba\u61c9\u7528\u7a0b\u5e8f\u6dfb\u52a0\u89f8\u63a7\u4e8b\u4ef6\u548c\u624b\u52e2\u8fa8\u8b58\u529f\u80fd\u3002\u96a8\u8457 Swift \u8a9e\u8a00\u7684\u6f14\u9032\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 2025 \u6700\u65b0\u7684 Swift \u8a9e\u6cd5\u4f86\u5be6\u73fe UITapGestureRecognizer \u7684\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u5be6\u4f5c\u7bc4\u4f8b\u3001\u932f\u8aa4\u6392\u9664\u548c\u5ef6\u4f38\u61c9\u7528\u7684\u5efa\u8b70\u3002<\/p>\n<h2>\u6b65\u9a5f\u4e00\uff1a\u8a2d\u7f6e UIView<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728 Storyboard \u4e2d\u5efa\u7acb\u4e00\u500b <strong>UIView<\/strong>\uff0c\u4e26\u5c07\u5b83\u62d6\u66f3\u5230 <strong>ViewController<\/strong> \u4e2d\u3002\u63a5\u8457\uff0c\u5c07\u5b83\u7684 <strong>Class<\/strong> \u8a2d\u5b9a\u70ba <strong>UIView<\/strong>\uff0c\u4e26\u5c07\u5b83\u7684 <strong>Identifier<\/strong> \u8a2d\u5b9a\u70ba <strong>myView<\/strong>\u3002<\/p>\n<h2>\u6b65\u9a5f\u4e8c\uff1a\u6dfb\u52a0 UITapGestureRecognizer<\/h2>\n<p>\u63a5\u8457\uff0c\u5728 <strong>ViewController<\/strong> \u4e2d\u5efa\u7acb\u4e00\u500b <strong>UITapGestureRecognizer<\/strong> \u7269\u4ef6\uff0c\u4e26\u5c07\u5b83\u6307\u5b9a\u7d66\u525b\u525b\u5efa\u7acb\u7684 <strong>UIView<\/strong>\u3002\u4ee5\u4e0b\u662f\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))\nif let myView = self.view.viewWithTag(1) as? UIView {\n    myView.addGestureRecognizer(tapGesture)\n}\n<\/code><\/pre>\n<h2>\u6b65\u9a5f\u4e09\uff1a\u8655\u7406\u89f8\u63a7\u4e8b\u4ef6<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5728 <strong>ViewController<\/strong> \u4e2d\u5efa\u7acb\u4e00\u500b <strong>handleTap<\/strong> \u7684\u51fd\u5f0f\uff0c\u4ee5\u8655\u7406\u89f8\u63a7\u4e8b\u4ef6\u3002\u4ee5\u4e0b\u662f\u8655\u7406\u89f8\u63a7\u7684\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">@objc func handleTap(sender: UITapGestureRecognizer) {\n    let alertController = UIAlertController(title: \"\u63d0\u793a\", message: \"\u60a8\u5df2\u7d93\u6309\u4e0b UIView\", preferredStyle: .alert)\n    let okAction = UIAlertAction(title: \"\u78ba\u5b9a\", style: .default, handler: nil)\n    alertController.addAction(okAction)\n    self.present(alertController, animated: true, completion: nil)\n}\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><strong>\u624b\u52e2\u672a\u80fd\u8b58\u5225\uff1a<\/strong>\u78ba\u4fdd\u60a8\u7684 <strong>UIView<\/strong> \u662f\u53ef\u4e92\u52d5\u7684\uff0c\u4e26\u4e14\u6c92\u6709\u5176\u5b83\u624b\u52e2\u8b58\u5225\u5668\u6c96\u7a81\u3002<\/li>\n<li><strong>\u672a\u80fd\u6b63\u78ba\u9023\u7d50\uff1a<\/strong>\u6aa2\u67e5\u60a8\u7684 IBOutlet \u548c IBOutletCollection \u662f\u5426\u6b63\u78ba\u9023\u7d50\u5230\u60a8\u7684 UIView\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u89f8\u63a7\u4e8b\u4ef6\uff0c\u60a8\u9084\u53ef\u4ee5\u64f4\u5c55 UITapGestureRecognizer \u7684\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u7d50\u5408\u591a\u500b\u624b\u52e2\u8b58\u5225\u5668\uff0c\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u4ea4\u4e92\u3002<\/li>\n<li>\u4f7f\u7528 <strong>UIPinchGestureRecognizer<\/strong> \u548c <strong>UISwipeGestureRecognizer<\/strong> \u4f86\u589e\u5f37\u61c9\u7528\u7684\u4e92\u52d5\u6027\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u89f8\u63a7\u4e8b\u4ef6\u8207\u624b\u52e2\u8fa8\u8b58-UITapGestureRecognizer_1675475675.295072.webp\" alt=\"Swift \u89f8\u63a7\u4e8b\u4ef6\u8207\u624b\u52e2\u8fa8\u8b58 (UITapGestureRecognizer)\" title=\"Swift, \u89f8\u63a7\u4e8b\u4ef6, \u624b\u52e2\u8fa8\u8b58, UITapGestureRecognizer, \u7a0b\u5f0f\u78bc\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&#038;A)<\/h2>\n<h3>1. UITapGestureRecognizer \u548c\u5176\u4ed6\u624b\u52e2\u8b58\u5225\u5668\u6709\u4f55\u4e0d\u540c\uff1f<\/h3>\n<p>UITapGestureRecognizer \u5c08\u6ce8\u65bc\u8b58\u5225\u8f15\u89f8\u4e8b\u4ef6\uff0c\u800c\u5176\u4ed6\u624b\u52e2\u8b58\u5225\u5668\u5982 UIPinchGestureRecognizer \u5247\u53ef\u4ee5\u8b58\u5225\u7e2e\u653e\u624b\u52e2\uff0c\u5169\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u7d50\u5408\u4f7f\u7528\u3002<\/p>\n<h3>2. \u9047\u5230\u624b\u52e2\u672a\u80fd\u8b58\u5225\u7684\u60c5\u6cc1\u600e\u9ebc\u8fa6\uff1f<\/h3>\n<p>\u78ba\u8a8d\u60a8\u7684 UIView \u662f\u53ef\u4e92\u52d5\u7684\uff0c\u4e26\u6aa2\u67e5\u662f\u5426\u6709\u5176\u4ed6\u624b\u52e2\u8b58\u5225\u5668\u5e72\u64fe\u4e86\u8a72\u624b\u52e2\u7684\u8b58\u5225\u3002<\/p>\n<h3>3. \u5982\u4f55\u5728\u624b\u52e2\u8b58\u5225\u5668\u4e2d\u7372\u53d6\u89f8\u6478\u4f4d\u7f6e\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>location(in:)<\/code> \u65b9\u6cd5\u4f86\u7372\u53d6\u89f8\u6478\u7684\u5ea7\u6a19\u4f4d\u7f6e\uff0c\u4f8b\u5982\uff1a<code>let touchLocation = sender.location(in: myView)<\/code><\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39Swift\u4e2dUITapGestureRecognizer\u7684\u4f7f\u7528\uff0c\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528\u624b\u52e2\u8fa8\u8b58\u4f86\u8655\u7406\u89f8\u63a7\u4e8b\u4ef6\uff0c\u4e26\u63d0\u4f9b\u7a0b\u5f0f\u78bc\u793a\u4f8b\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n","protected":false},"author":1,"featured_media":7417,"comment_status":"open","ping_status":"closed","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-7418","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u89f8\u63a7\u4e8b\u4ef6\u8207\u624b\u52e2\u8fa8\u8b58-UITapGestureRecognizer_1675475675.295072.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1VE","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7418","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=7418"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7418\/revisions"}],"predecessor-version":[{"id":13546,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7418\/revisions\/13546"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7417"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}