{"id":581,"date":"2023-02-25T19:35:25","date_gmt":"2023-02-25T11:35:25","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=581"},"modified":"2023-04-29T20:31:57","modified_gmt":"2023-04-29T12:31:57","slug":"swift-ui-slider-uislider","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-ui-slider-uislider\/","title":{"rendered":"Swift UI &#8211; \u6ed1\u5757\uff08UISlider\uff09"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<h3>\u6ed1\u5757\u7684\u521b\u5efa<\/h3>\n<p><!-- more --><\/p>\n<pre><code class=\"language-swift\">let slider = UISlider(frame:CGRect(x:0, y:0, width:300, height:50))\nslider.center = self.view.center\nslider.minimumValue = 0  \/\/\u6700\u5c0f\u503c\nslider.maximumValue = 1  \/\/\u6700\u5927\u503c\nslider.value = 0.5  \/\/\u5f53\u524d\u9ed8\u8ba4\u503c\nself.view.addSubview(slider)<\/code><\/pre>\n<h3>\u8bbe\u7f6e\u6ed1\u5757\u7684\u503c\uff0c\u540c\u65f6\u6709\u52a8\u753b<\/h3>\n<pre><code class=\"language-swift\">slider.setValue(0.8,animated:true)<\/code><\/pre>\n<h3>\u6ed1\u5757\u503c\u6539\u53d8\u76d1\u542c<\/h3>\n<pre><code class=\"language-swift\">slider.isContinuous = false  \/\/\u6ed1\u5757\u6ed1\u52a8\u505c\u6b62\u540e\u624d\u89e6\u53d1ValueChanged\u4e8b\u4ef6\nslider.addTarget(self, action:#selector(sliderDidChanged(_:)), for:UIControlEvents.valueChanged)\n\n@objc func sliderDidChanged(_ slider:UISlider){\n    print(slider.value)\n}<\/code><\/pre>\n<h3>\u6ed1\u5757\u5de6\u53f3\u4e24\u8fb9\u69fd\u7684\u989c\u8272<\/h3>\n<pre><code class=\"language-swift\">slider.minimumTrackTintColor = UIColor.red  \/\/\u5de6\u8fb9\u69fd\u7684\u989c\u8272\nslider.maximumTrackTintColor = UIColor.green  \/\/\u53f3\u8fb9\u69fd\u7684\u989c\u8272<\/code><\/pre>\n<h3>\u6ed1\u5757\u540e\u9762\u69fd\u7ebf\u4e24\u4fa7\u6dfb\u52a0\u56fe\u6807<\/h3>\n<pre><code class=\"language-swift\">slider.minimumValueImage = UIImage(named:&quot;voice+&quot;)  \/\/\u5de6\u8fb9\u56fe\u6807\nslider.maximumValueImage = UIImage(named:&quot;voice-&quot;)  \/\/\u53f3\u8fb9\u56fe\u6807<\/code><\/pre>\n<h3>\u81ea\u5b9a\u4e49\u6ed1\u5757\u7ec4\u4ef6\u56fe\u7247<\/h3>\n<pre><code class=\"language-swift\">\/\/\u8bbe\u7f6e\u6ed1\u5757\u53f3\u8fb9\u90e8\u5206\u7684\u56fe\u7247\nslider.setMaximumTrackImage(UIImage(named:&quot;slider_max&quot;), for: .normal)\n\/\/\u8bbe\u7f6e\u6ed1\u5757\u5de6\u8fb9\u90e8\u5206\u7684\u56fe\u7247\nslider.setMinimumTrackImage(UIImage(named:&quot;slider_min&quot;), for: .normal)\n\/\/\u8bbe\u7f6e\u6ed1\u5757\u7684\u56fe\u7247\nslider.setThumbImage(UIImage(named:&quot;slider_thumb&quot;), for: .normal)\n\n\/\/\u8bbe\u7f6e\u6ed1\u5757\u53f3\u8fb9\u90e8\u5206\u7684\u56fe\u7247-\u4f7f\u7528\u4e09\u5bab\u683c\u7f29\u653e\uff08\u5de6\u53f314\u50cf\u7d20\u4e0d\u53d8\uff0c\u4e2d\u95f4\u7f29\u653e\uff09\nlet imgTrackRight = UIImage(named:&quot;slider_max&quot;)\nlet imgRight = imgTrackRight!.stretchableImage(withLeftCapWidth: 14, topCapHeight:0)\nslider.setMaximumTrackImage(imgRight, for: .normal)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 \u6ed1\u5757\u7684\u521b\u5efa let slider = UISlider(frame:CGRect [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[171],"tags":[],"class_list":["post-581","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=581"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/581\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}