{"id":7461,"date":"2025-05-31T14:05:35","date_gmt":"2025-06-05T06:32:34","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7461"},"modified":"2025-06-05T14:05:35","modified_gmt":"2025-06-05T06:32:34","slug":"https-badgameshow-com-steven-401","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-401\/","title":{"rendered":"2025 \u6700\u65b0 Swift \u5716\u7247\u61f8\u6d6e\u6548\u679c\u6559\u5b78 \ud83d\udcf7\ud83d\udd19"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u5716\u7247\u61f8\u6d6e\u6548\u679c, iOS \u958b\u767c, \u624b\u52e2\u8b58\u5225\"><\/p>\n<h2>\u5f15\u8a00<\/h2>\n<p>\u5728 iOS \u958b\u767c\u4e2d\uff0c\u4f7f\u7528 Swift \u5be6\u73fe\u5716\u7247\u61f8\u6d6e\u6548\u679c\u662f\u4e00\u500b\u975e\u5e38\u6709\u8da3\u4e14\u5be6\u7528\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u589e\u5f37\u4f60\u7684 App \u754c\u9762\u7684\u4e92\u52d5\u6027\u8207\u5438\u5f15\u529b\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u5be6\u73fe\u9019\u4e00\u7279\u6548\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u6559\u5b78\u6d41\u7a0b\u548c\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<h2>\u6b65\u9a5f\u4e00\uff1a\u5275\u5efa\u5716\u7247\u8996\u5716<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5efa\u7acb\u4e00\u500b <strong>UIImageView<\/strong> \u4f86\u653e\u7f6e\u5716\u7247\uff0c\u4e26\u8a2d\u5b9a\u5176\u70ba\u53ef\u62d6\u52d5\u7684\u72c0\u614b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))\nimageView.image = UIImage(named: \"image\")\nimageView.isUserInteractionEnabled = true\nview.addSubview(imageView)<\/code><\/pre>\n<h2>\u6b65\u9a5f\u4e8c\uff1a\u6dfb\u52a0\u624b\u52e2\u8b58\u5225\u5668<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b <strong>UIPanGestureRecognizer<\/strong> \u4f86\u6aa2\u6e2c\u7528\u6236\u7684\u62d6\u52d5\u624b\u52e2\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture))\nimageView.addGestureRecognizer(panGesture)<\/code><\/pre>\n<h2>\u6b65\u9a5f\u4e09\uff1a\u5be6\u73fe\u624b\u52e2\u8655\u7406\u908f\u8f2f<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5be6\u73fe <strong>handlePanGesture<\/strong> \u65b9\u6cd5\u4f86\u66f4\u65b0\u5716\u7247\u7684\u4f4d\u7f6e\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">@objc func handlePanGesture(gesture: UIPanGestureRecognizer) {\n    let translation = gesture.translation(in: view)\n    gesture.view?.center = CGPoint(x: gesture.view!.center.x + translation.x, y: gesture.view!.center.y + translation.y)\n    gesture.setTranslation(CGPoint.zero, in: view)\n}<\/code><\/pre>\n<h2>\u5b8c\u6574\u4ee3\u78bc\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u78bc\u793a\u4f8b\uff0c\u53ef\u4ee5\u76f4\u63a5\u61c9\u7528\u65bc\u4f60\u7684 iOS \u9805\u76ee\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">import UIKit\n\nclass ViewController: UIViewController {\n    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        imageView.image = UIImage(named: \"image\")\n        imageView.isUserInteractionEnabled = true\n        view.addSubview(imageView)\n\n        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture))\n        imageView.addGestureRecognizer(panGesture)\n    }\n\n    @objc func handlePanGesture(gesture: UIPanGestureRecognizer) {\n        let translation = gesture.translation(in: view)\n        gesture.view?.center = CGPoint(x: gesture.view!.center.x + translation.x, y: gesture.view!.center.y + translation.y)\n        gesture.setTranslation(CGPoint.zero, in: view)\n    }\n}<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<ul>\n<li><strong>\u5716\u7247\u672a\u986f\u793a\uff1a<\/strong>\u78ba\u4fdd\u5716\u7247\u540d\u7a31\u6b63\u78ba\u4e14\u5df2\u6dfb\u52a0\u81f3\u8cc7\u6e90\u4e2d\u3002<\/li>\n<li><strong>\u624b\u52e2\u7121\u6cd5\u8b58\u5225\uff1a<\/strong>\u6aa2\u67e5 <strong>isUserInteractionEnabled<\/strong> \u662f\u5426\u8a2d\u70ba true\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u4f60\u53ef\u4ee5\u9032\u4e00\u6b65\u64f4\u5c55\u6b64\u529f\u80fd\uff0c\u4f8b\u5982\u6dfb\u52a0\u5716\u7247\u7e2e\u653e\u6216\u65cb\u8f49\u6548\u679c\uff0c\u8b93\u7528\u6236\u6709\u66f4\u8c50\u5bcc\u7684\u4ea4\u4e92\u9ad4\u9a57\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5716\u7247\u61f8\u6d6e\u6548\u679c-\ud83d\udcf7\ud83d\udd19_1675476265.2952461.webp\" alt=\"Swift \u5716\u7247\u61f8\u6d6e\u6548\u679c \ud83d\udcf7\ud83d\udd19\" title=\"Swift \u5716\u7247\u61f8\u6d6e\u6548\u679c\u5c55\u793a\uff0c\u5c55\u793a\u5982\u4f55\u5728 iOS App \u4e2d\u5be6\u73fe\u5716\u7247\u61f8\u6d6e\u529f\u80fd\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u9019\u500b\u6548\u679c\u53ef\u4ee5\u5728\u6240\u6709\u7248\u672c\u7684 iOS \u4e0a\u4f7f\u7528\u55ce\uff1f<\/h3>\n<p>A1: \u662f\u7684\uff0c\u9019\u500b\u5716\u7247\u61f8\u6d6e\u6548\u679c\u53ef\u4ee5\u5728 iOS 9 \u53ca\u4ee5\u4e0a\u7248\u672c\u4e0a\u4f7f\u7528\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u70ba\u61f8\u6d6e\u5716\u7247\u6dfb\u52a0\u52d5\u756b\u6548\u679c\uff1f<\/h3>\n<p>A2: \u4f60\u53ef\u4ee5\u4f7f\u7528 <strong>UIView.animate<\/strong> \u65b9\u6cd5\u4f86\u70ba\u5716\u7247\u6dfb\u52a0\u79fb\u52d5\u6216\u7e2e\u653e\u7684\u52d5\u756b\u6548\u679c\u3002<\/p>\n<h3>Q3: \u6211\u53ef\u4ee5\u5c0d\u591a\u500b\u5716\u7247\u4f7f\u7528\u61f8\u6d6e\u6548\u679c\u55ce\uff1f<\/h3>\n<p>A3: \u53ef\u4ee5\uff0c\u4f60\u53ea\u9700\u70ba\u6bcf\u500b\u5716\u7247\u5275\u5efa\u4e00\u500b <strong>UIImageView<\/strong> \u548c\u624b\u52e2\u8b58\u5225\u5668\u5373\u53ef\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5728Swift\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528UIViewController\u548cUIScrollView\u4f86\u5be6\u73fe\u5716\u7247\u61f8\u6d6e\u6548\u679c\uff0c\u8b93\u5716\u7247\u5728\u6ed1\u52d5\u6642\u4fdd\u6301\u56fa\u5b9a\u7684\u4f4d\u7f6e\uff0c\u4e26\u4e14\u53ef\u4ee5\u8a2d\u7f6e\u5716\u7247\u7684\u5927\u5c0f\u548c\u4f4d\u7f6e\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u4f86\u5be6\u73fe\u5716\u7247\u61f8\u6d6e\u6548\u679c\u3002<\/p>\n","protected":false},"author":1,"featured_media":7460,"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-7461","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-\u5716\u7247\u61f8\u6d6e\u6548\u679c-\ud83d\udcf7\ud83d\udd19_1675476265.2952461.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Wl","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7461","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=7461"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7461\/revisions"}],"predecessor-version":[{"id":13478,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7461\/revisions\/13478"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7460"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}