【问题标题】:Subclass of UIButton animate sublayer with titleLabelUIButton 的子类使用 titleLabel 动画子层
【发布时间】: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


    【解决方案1】:

    如果我改写 touchesBegan 和 touchesEnded,这似乎对我有用。

        override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
            super.touchesBegan(touches, withEvent: event)
            innerShadowLayer.opacity = 0.5
        }
    
        override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
            super.touchesEnded(touches, withEvent: event)
            innerShadowLayer.opacity = 1.0
        }
    

    【讨论】:

    • 但是 UIButton 有一个特殊的效果,当你按住按钮时,它会改变文本的 alpha。这不会那样做。有什么建议吗?
    猜你喜欢
    • 2018-11-05
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    相关资源
    最近更新 更多