【发布时间】:2015-03-11 13:18:43
【问题描述】:
我正在创建UIButton 的子类,以便在按钮层上有一个自定义子层。
我将此按钮设置为 System 在情节提要中键入,然后将 UIButton 中的类替换为我在情节提要中 Identity inspector 下的子类。
我使用System 类型按钮而不是自定义来获得titleLabel 上的淡入淡出动画。
现在,我想在 titleLabel 淡出时也淡出子图层。
我尝试了begin/continue/endTrackingWithTouch: 方法,但是当titleLabel 更改时它不会更改。
class TAButton:UIButton {
let innerShadowLayer = CFInnerShadow() // Custom class of CAGradientLayer
override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent) -> Bool {
innerShadowLayer.opacity = 0.5
return super.beginTrackingWithTouch(touch, withEvent: event)
}
override func continueTrackingWithTouch(touch: UITouch, withEvent event: UIEvent) -> Bool {
let con = super.continueTrackingWithTouch(touch, withEvent: event)
println(con) // I expected this to change to false when titleLabel fades but I was wrong, its always true
return con
}
override func endTrackingWithTouch(touch: UITouch, withEvent event: UIEvent) {
innerShadowLayer.opacity = 1.0
super.endTrackingWithTouch(touch, withEvent: event)
}
func setup() {
self.setBackgroundGradient(UIColor(hex: 0x4A9BCB, alpha: 0.25), color2: UIColor(hex: 0xBAE5FF, alpha: 0.25)) // Custom function Im using
self.makeRoundSides() // Custom function Im using
innerShadowLayer.frame = self.bounds
innerShadowLayer.cornerRadius = self.layer.cornerRadius
innerShadowLayer.innerShadowRadius = 10
innerShadowLayer.innerShadowOpacity = 0.6
innerShadowLayer.innerShadowColor = UIColor(hex: 0x69FFF7, alpha: 1.0).CGColor
self.layer.addSublayer(innerShadowLayer)
}
override func layoutSubviews() {
super.layoutSubviews()
setup()
}
}
【问题讨论】:
标签: ios swift uibutton subclass uicontrol