按下时反转按钮颜色?

时间:2017-05-24 16:12:35

标签: ios swift uibutton

我想尝试使用这种风格的按钮:

enter image description here

这是我到目前为止所拥有的:

otherbuttons

但是,我不确定在按下按钮时如何为按钮获得正确的颜色,就像示例中的中间按钮一样。我设法通过使用:

使图像的颜色变白
button.setImage(UIImage("Image_White_Version"), for: .highlighted)

但我不知道如何用颜色填充按钮的其余部分。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:1)

您可以为特定状态设置UIButton的背景图像,如下所示:

readSlice

这是一个从颜色创建图像的功能(放在UIImage的扩展名内):

let image = UIImage.imageWithColor(someColor, size: CGSize(width: 1.0, height: 1.0))
btn.setBackgroundImage(image, for: .highlighted)

答案 1 :(得分:0)

尝试使用下一种方法:

这是如何实现这一目标的快捷方式。主要想法使用按钮背景颜色和图像色调颜色。但是更好的方法是在基础UIControl上准备新的自定义控件。如果它不会让我感到满意,我会为你做好准备。

    var BgColorNormalIdentifier = "BackgroundColorNormal"
    var BgColorHighlightedIdentifier = "BackgroundColorHighlighted"
    var ImgColorNormalIdentifier = "ImageColorNormal"
    var ImgColorHighlightedIdentifier = "ImageColorHighlighted"

    public extension UIButton
    {
        public func backgroundColorForStates(normal: UIColor, highlighted: UIColor)
        {
            backgroundColor = normal

            addTarget(self, action:#selector(UIButton.buttonTouchUpInside(_:)), for:.touchUpInside)
            addTarget(self, action:#selector(UIButton.buttonTouchUpOutside(_:)), for:.touchUpOutside)
            addTarget(self, action:#selector(UIButton.buttonTouchDown(_:)), for:.touchDown)
            addTarget(self, action:#selector(UIButton.buttonTouchCancel(_:)), for:.touchCancel)

            objc_setAssociatedObject(self, &BgColorNormalIdentifier, normal, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            objc_setAssociatedObject(self, &BgColorHighlightedIdentifier, highlighted, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            objc_setAssociatedObject(self, &ImgColorNormalIdentifier, highlighted, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            objc_setAssociatedObject(self, &ImgColorHighlightedIdentifier, normal, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }

        public func imageForState (image: UIImage?, for state: UIControlState) {
            self.setImage(image?.withRenderingMode(.alwaysTemplate), for: state)
        }

        public func normalBackgroundColor() -> UIColor?
        {
            return objc_getAssociatedObject(self, &BgColorNormalIdentifier) as? UIColor
        }

        public func highlightedBackgroundColor() -> UIColor?
        {
            return objc_getAssociatedObject(self, &BgColorHighlightedIdentifier) as? UIColor
        }

        public func normalImageColor() -> UIColor?
        {
            return objc_getAssociatedObject(self, &ImgColorNormalIdentifier) as? UIColor
        }

        public func highlightedImageColor() -> UIColor?
        {
            return objc_getAssociatedObject(self, &ImgColorHighlightedIdentifier) as? UIColor
        }

        func buttonTouchUpInside(_ sender: UIButton)
        {
            sender.backgroundColor = normalBackgroundColor()
            sender.tintColor = normalImageColor()
        }

        func buttonTouchUpOutside(_ sender: UIButton)
        {
            sender.backgroundColor = normalBackgroundColor()
            sender.tintColor = normalImageColor()
        }

        func buttonTouchDown(_ sender: UIButton)
        {
            sender.backgroundColor = highlightedBackgroundColor()
            sender.tintColor = highlightedBackgroundColor()
        }

        func buttonTouchCancel(_ sender: UIButton)
        {
            sender.backgroundColor = normalBackgroundColor()
            sender.tintColor = normalImageColor()
        }
    }