触摸结束时如何更改 UISlider 拇指外观 [英] How to change UISlider Thumb Appearance when Touch Ends

查看:18
本文介绍了触摸结束时如何更改 UISlider 拇指外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过调用 .thumbTintColor 来改变 UISlider 的颜色

I am changing the color of a UISlider by calling .thumbTintColor

@IBAction func slider1Master(sender: AnyObject) {

    slider1.thumbTintColor = UIColor.orangeColor()}

它可以工作,但我希望在触摸结束(用户抬起手指)时颜色变回原来的状态.

It works, but I want the color to change back to it's original state when the touch ends (user lifts finger).

有人有什么建议吗?谢谢.

Does anyone have any suggestions? Thank you.

推荐答案

您可以改用setThumbImage".然后,您可以选择为特定的动作状态设置图像.对于图像,只需使用您想要的颜色创建一个更圆的图像.

You can use "setThumbImage" instead. Then you have the option of setting an image for a specific state of action. For the image, just create a rounder image with the color you desire.

//Creating an Image with rounded corners:

extension UIImage {
    class 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)
    }
    class func imageFromLayer(layer: CALayer) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, UIScreen.mainScreen().scale)
        layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let outputImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return outputImage
    }
}


//Setting the image for a selected state of UISlider:

func setupSlider() {
        let size:CGFloat = 12
        let highlightedStateOrangeColorImage = UIImage.createThumbImage(size, color: UIColor.orangeColor())
        let defaultStateBlueColorImage = UIImage.createThumbImage(size, color: UIColor.blueColor())
        self.slider.setThumbImage(highlightedStateOrangeColorImage, forState: UIControlState.Highlighted)
        self.slider.setThumbImage(defaultStateBlueColorImage, forState: UIControlState.Normal)
}

这篇关于触摸结束时如何更改 UISlider 拇指外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆