1

Background:

The UIButton control has a property “Shows Touch On Highlight” that can be set in the Storyboard. When this option is checked, and the UIButton is touched, a white glow is visible on the UIButton text.

Question:

How can I adjust the radius size of the UIButton glow when touched?

jscs
  • 63,694
  • 13
  • 151
  • 195
user4806509
  • 2,925
  • 5
  • 37
  • 72

2 Answers2

0

Using code from my previous answer Need a Glowing Animation around a button I had been working, adding some customizations in order to solve your answer and here is the results, If you set animateAllways = false this custom button will behave as you need

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

    }

}

Hope this helps you

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
  • Thanks, and sorry for the delay, this has shown me some different techniques, but it doesn't adjust the `Touch On Highlight` but instead adds a separate layer. I'm specifically focusing on only adjusting the built-in glow of `Touch On Highlight`. – user4806509 Jul 17 '17 at 13:54
  • 1
    @user4806509 I don't know if the built-in glow on highlight can be modified, anyway if you solve this let me know to learn it – Reinier Melian Jul 17 '17 at 14:33
  • I'm starting to think that as well. The glow changes size automatically based on the font size of the `UIButton` which suggests to me that it may have no custom option to adjust the size further. Will keep you updated if I learn more! Cheers. – user4806509 Jul 17 '17 at 14:41
-1

Connect your button to an @IBOutlet and call it button, then you can do that by putting this code inside viewDidLoad method:

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

And you can change the customization in the way you like. Good luck!

Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
  • Thanks, it's close, but it doesn't adjust the Touch On Highlight but instead adds a shadow when not touched and a second glow when touched in addition to the Touch On Highlight itself. I'm specifically focusing just on the Touch On Highlight. – user4806509 Jul 08 '17 at 14:11
  • (Side note, this suggestion was helpful to learn a different technique, so thanks for that.) – user4806509 Jul 08 '17 at 14:19