如何突出显示没有文本或背景的自定义UIButton? [英] How to highlight a custom UIButton that has no text or background?

查看:77
本文介绍了如何突出显示没有文本或背景的自定义UIButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个自定义UIButton,因为我想绘制自己的矢量按钮图标,而不是依靠文本字符或位图图像.

I am trying to make a custom UIButton because I want to draw my own vector button icons instead of relying on text characters or bitmap images.

所以...我尝试过:

import UIKit

@IBDesignable
class CustomPlusButton: UIButton {

/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
    // Drawing code
}
*/


func myPlusSign() -> UIBezierPath {

    //vars to factor out later
    let G = bounds.width / 5

    let plusPath = UIBezierPath()


    let startPoint = CGPoint(x:2*G,y:G)

    plusPath.move(to: startPoint)
    plusPath.addLine(to: CGPoint(x:2*G,y:2*G))
    plusPath.addLine(to: CGPoint(x:G,y:2*G))
    plusPath.addLine(to: CGPoint(x:G,y:3*G))
    plusPath.addLine(to: CGPoint(x:2*G,y:3*G))
    plusPath.addLine(to: CGPoint(x:2*G,y:4*G))
    plusPath.addLine(to: CGPoint(x:3*G,y:4*G))
    plusPath.addLine(to: CGPoint(x:3*G,y:3*G))
    plusPath.addLine(to: CGPoint(x:4*G,y:3*G))
    plusPath.addLine(to: CGPoint(x:4*G,y:2*G))
    plusPath.addLine(to: CGPoint(x:3*G,y:2*G))
    plusPath.addLine(to: CGPoint(x:3*G,y:G))
    plusPath.addLine(to: startPoint)

    plusPath.close()

    plusPath.fill()

    return plusPath



}

override func draw(_ rect: CGRect) {

    let path = myPlusSign()

    path.stroke()

}
}

我将Button实例拖到情节提要中,并将其类型更改为自定义类.

I dragged a Button instance into the storyboard and changed it's type to my custom class.

在情节提要中,当我运行该应用程序时,从向量的外观以及它如何适合按钮空间的角度来看,它看起来都很棒,但是当按普通按钮的方式触摸时,它不会突出显示.

In the storyboard, and when I run the app, it looks great as far as how the vector looks and how it fits into the button space, but it doesn't highlight when touched the way a normal button does.

据我了解,通常您会使用属性检查器的状态配置"部分来控制突出显示时的行为,但它仅允许控制文本颜色,阴影颜色,图像或背景.

As I understand it, normally you would use the "State config" section of the attributes inspector to control the behavior when highlighted, but it only allows control of text color, shadow color, image, or background.

我的自定义图标不是这些东西...那么如何引用它呢? draw(_ rect)的输出是什么?如何突出显示它?

My custom icon is none of these things... so how can I refer to it? What is the output of draw(_ rect) referred to as and how can I highlight it?

我还希望它像普通按钮一样退回到正常状态,因此希望有某种方法可以合并/重用普通按钮的突出显示行为而不会完全覆盖它?

I would also like it to fade back to normal state like a normal button does so hopefully there is some way to incorporate/reuse the highlight behavior of a normal button without fully overriding it?

谢谢!

推荐答案

对于可能的解决方案,您可以在按钮的已覆盖isHighlighteddidSet中修改按钮的alpha.

For a possible solution, you can modify the alpha of the button in the didSet of your button's overridden isHighlighted.

override var isHighlighted: Bool {
    didSet {
        alpha = isHighlighted ? 0.7 : 1
    }
}

这篇关于如何突出显示没有文本或背景的自定义UIButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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