我正在开发一个应用程序,我有一个自定义按钮类(我在StackOverflow上找到了这个类 - 归功于原始海报)。我在按钮上添加了一个CAShapelayer,使其形状像圆角矩形按钮。这很好用,但我想在用户用手指按住按钮时突出显示该按钮,我遇到了问题。当我覆盖突出显示的颜色时,它只突出显示我的圆角矩形图层的图层。
有人可以帮助我了解我需要做些什么才能使我的自定义按钮正确显示?我在iOS8.1中的Swift中构建它。非常感谢!
自定义按钮代码:
public var roundRectCornerRadius: CGFloat = 8 {
didSet {
self.setNeedsLayout()
}
}
/// background of the rounded lyer
public var roundRectColor: UIColor = UIColor.whiteColor() {
didSet {
self.setNeedsLayout()
}
}
override public func layoutSubviews() {
super.layoutSubviews()
layoutRoundRectLayer()
}
// how do i get this into the roundrectlayer?
override public var highlighted: Bool {
get {
return super.highlighted
}
set {
if newValue {
backgroundColor = UIColor.blackColor()
}
else {
backgroundColor = UIColor.whiteColor()
}
super.highlighted = newValue
}
}
private var roundRectLayer: CAShapeLayer?
private func layoutRoundRectLayer() {
if let existingLayer = roundRectLayer {
existingLayer.removeFromSuperlayer()
}
let shapeLayer = CAShapeLayer()
shapeLayer.path = UIBezierPath(roundedRect: self.bounds, cornerRadius: roundRectCornerRadius).CGPath
shapeLayer.fillColor = roundRectColor.CGColor
self.layer.insertSublayer(shapeLayer, atIndex: 0)
self.roundRectLayer = shapeLayer
}