ios - 没有图像的自定义 UISlider
<p><p>我正在尝试在 iOS7 中调整 <code>UISlider</code> 拇指的大小,但我不能。我知道可以使用 <code>setThumbImage</code> 但我不想使用任何图像。</p>
<pre><code>setThumbImage:
</code></pre>
<p>我想在没有图像的情况下更改 <code>UISlider</code> 拇指的大小和颜色。我可以这样做吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>下面的 SWIFT 代码可让您轻松更改 sliderslider 的大小和颜色:</p>
<pre><code>override func viewDidLoad(){
super.viewDidLoad()
self.updateSlider(self.mySlider, thumbSize: 16, color: UIColor.redColor())
}
func updateSlider(slider: UISlider, thumbSize: CGFloat, color: UIColor){
let thumbImage = createThumbImage(thumbSize, color: color)
slider.setThumbImage(thumbImage, forState: UIControlState.Normal)
slider.setThumbImage(thumbImage, forState: UIControlState.Highlighted)
}
func createThumbImage(size: CGFloat, color: UIColor) -> UIImage {
let layerFrame = CGRectMake(0, 0, size, size)
let shapeLayer = CAShapeLayer()
shapeLayer.path = CGPathCreateWithEllipseInRect(layerFrame.insetBy(dx: 1, dy: 1), nil)
shapeLayer.fillColor = color.CGColor
shapeLayer.strokeColor = color.colorWithAlphaComponent(0.65).CGColor
let layer = CALayer.init()
layer.frame = layerFrame
layer.addSublayer(shapeLayer)
return self.imageFromLayer(layer)
}
func imageFromLayer(layer: CALayer) -> UIImage {
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, UIScreen.mainScreen().scale)
layer.renderInContext(UIGraphicsGetCurrentContext()!)
let outputImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return outputImage
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 没有图像的自定义 UISlider,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/22815531/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/22815531/
</a>
</p>
页:
[1]