在触摸时调整UIButton的高光辉光半径大小

时间:2017-07-08 13:26:08

标签: ios cocoa-touch uibutton

背景

UIButton控件具有可在故事板中设置的属性“显示高亮显示触摸”。选中此选项并触摸UIButton后,UIButton文字上会显示白色光晕。

问题:

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

2 个答案:

答案 0 :(得分:0)

使用我之前回答中的代码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")

    }

}

希望这有助于你

答案 1 :(得分:-1)

将您的按钮连接到@IBOutlet并将其命名为button,然后您可以将此代码放入viewDidLoad方法中来执行此操作:

button.layer.shadowColor = UIColor.red.cgColor
button.layer.shadowRadius = 30.0
button.layer.shadowOpacity = 0.9
button.layer.shadowOffset = CGSize.zero
button.layer.masksToBounds = false

您可以按自己喜欢的方式更改自定义。祝你好运!

相关问题