【问题标题】:Adjust a UIButton's highlight glow radius size on touch在触摸时调整 UIButton 的高亮发光半径大小
【发布时间】:2017-12-12 16:39:52
【问题描述】:

背景:

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

问题:

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

【问题讨论】:

    标签: ios cocoa-touch uibutton


    【解决方案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
    

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

    【讨论】:

    • 谢谢,它已经接近了,但它不会调整 Touch On Highlight,而是在未触摸时添加阴影,在触摸时添加第二次发光,除了 Touch On Highlight 本身。我特别关注 Touch On Highlight。
    • (旁注,这个建议有助于学习不同的技术,所以谢谢。)
    【解决方案2】:

    使用我之前的答案 Need a Glowing Animation around a button 我一直在工作的代码,添加一些自定义项以解决您的答案,这是结果,如果您设置 animateAllways = false,此自定义按钮将按照您的需要运行

    //
    //  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")
    
        }
    
    }
    

    希望对你有帮助

    【讨论】:

    • 谢谢,抱歉耽搁了,这向我展示了一些不同的技术,但它没有调整Touch On Highlight,而是添加了一个单独的层。我特别关注只调整Touch On Highlight 的内置辉光。
    • @user4806509 不知道自带的高亮发光能不能改,反正解决了告诉我学习一下
    • 我也开始这么想了。发光会根据UIButton 的字体大小自动更改大小,这表明它可能没有自定义选项来进一步调整大小。如果我了解更多信息,会及时更新!干杯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 2012-01-03
    • 2014-09-05
    • 1970-01-01
    • 2011-12-08
    • 2018-10-01
    • 1970-01-01
    相关资源
    最近更新 更多