ios - 触摸时调整 UIButton 的高亮发光半径大小

标签 ios cocoa-touch uibutton

背景:

UIButton 控件有一个属性“Shows Touch On Highlight”,可以在 Storyboard 中设置。选中此选项并触摸 UIButton 时,UIButton 文本上会出现白光。

问题:

如何调整触摸时 UIButton 发光的半径大小?

最佳答案

使用我之前的回答 Need a Glowing Animation around a button 中的代码我一直在努力,添加一些自定义项以解决您的问题,结果如下,如果您设置 animateAllways = false,此自定义按钮将按照您的需要运行

enter image description here

//
//  GlowingButton.swift
//  NavigationButtonRotateQuestion
//
//  Created by Reinier Melian on 01/07/2017.
//  Copyright © 2017 Pruebas. All rights reserved.
//

import UIKit

@IBDesignable
class GlowingButton: UIButton {

    @IBInspectable var animDuration : CGFloat = 3
    @IBInspectable var cornerRadius : CGFloat = 5
    @IBInspectable var maxGlowSize : CGFloat = 10
    @IBInspectable var minGlowSize : CGFloat = 0
    @IBInspectable var glowColor : UIColor = nil ?? UIColor.red
    @IBInspectable var animateAllways : Bool = false
    fileprivate var animating : Bool = false

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.contentScaleFactor = UIScreen.main.scale
        self.layer.masksToBounds = false

        if(self.animateAllways){
            self.setupButtonForContinueAnimation()
            self.startAnimation()
        }else{
            self.setupButton()
        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        if(!self.animateAllways){
        let layerAnimation = CABasicAnimation(keyPath: "shadowRadius")
        layerAnimation.fromValue = minGlowSize
        layerAnimation.toValue = maxGlowSize
        layerAnimation.isAdditive = false
        layerAnimation.duration = CFTimeInterval(animDuration/2)
        layerAnimation.fillMode = kCAFillModeForwards
        layerAnimation.isRemovedOnCompletion = false
        self.layer.add(layerAnimation, forKey: "addGlowing")
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

        if(!self.animateAllways){
        let layerAnimation = CABasicAnimation(keyPath: "shadowRadius")
        layerAnimation.fromValue = maxGlowSize
        layerAnimation.toValue = minGlowSize
        layerAnimation.isAdditive = false
        layerAnimation.duration = CFTimeInterval(animDuration/2)
        layerAnimation.fillMode = kCAFillModeForwards
        layerAnimation.isRemovedOnCompletion = false
        self.layer.add(layerAnimation, forKey: "removeGlowing")
        }
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {

        if(!self.animateAllways){
        let layerAnimation = CABasicAnimation(keyPath: "shadowRadius")
        layerAnimation.fromValue = maxGlowSize
        layerAnimation.toValue = minGlowSize
        layerAnimation.isAdditive = false
        layerAnimation.duration = CFTimeInterval(animDuration/2)
        layerAnimation.fillMode = kCAFillModeForwards
        layerAnimation.isRemovedOnCompletion = false
        self.layer.add(layerAnimation, forKey: "removeGlowing")
        }
    }

    func setupButton()
    {
        self.layer.cornerRadius = cornerRadius
        self.layer.shadowPath = CGPath(roundedRect: self.bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius, transform: nil)
        self.layer.shadowRadius = 0
        self.layer.shadowColor = self.glowColor.cgColor
        self.layer.shadowOffset = CGSize.zero
        self.layer.shadowOpacity = 1
    }

    func setupButtonForContinueAnimation()
    {
        self.setupButton()
        self.layer.shadowRadius = maxGlowSize
    }

    func startAnimation()
    {
        let layerAnimation = CABasicAnimation(keyPath: "shadowRadius")
        layerAnimation.fromValue = maxGlowSize
        layerAnimation.toValue = minGlowSize
        layerAnimation.autoreverses = true
        layerAnimation.isAdditive = false
        layerAnimation.duration = CFTimeInterval(animDuration/2)
        layerAnimation.fillMode = kCAFillModeForwards
        layerAnimation.isRemovedOnCompletion = false
        layerAnimation.repeatCount = .infinity
        self.layer.add(layerAnimation, forKey: "glowingAnimation")

    }

}

希望对你有帮助

关于ios - 触摸时调整 UIButton 的高亮发光半径大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44986742/

相关文章:

ios - UILocalNotification 没有做任何事情

iphone - 相对于父 View 的底部定位 UIView?

iphone - View 禁用用户交互但 subview 启用(UIButtons)

cocoa-touch - 有没有办法在运行时获取Bundle ID?

ios - UIButton 背景图像在点击时不会改变

swift - ScrollView 上容器内的动态 UIButton 操作不起作用

ios - 从谷歌地图回调到我的应用程序

iphone - 仅当用户下拉表格时才显示搜索栏

ios - 使用自动布局 Swift 定位 uibutton

ios - 指定 View 大小何时依赖于多个 subview 的约束