Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a Glowing Animation around a button

In my application i have a big button. I want it to have a glowing effect around it. Can it be done using animations? I have tried using the image but it does not looks clean and appealing.

like image 683
Abhilasha Avatar asked Dec 07 '22 17:12

Abhilasha


1 Answers

I had been working in your question and this is my results,I define a custom subclass of UIButton,adding a CABasicAnimation animating shadowRadius property, setting autoreverses to true and repeatCount to infinity

Code

//
//  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 animateAlways : 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.animateAlways){
            self.setupButtonForContinueAnimation()
            self.startAnimation()
        }else{
            self.setupButton()
        }
    }

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

        if(!self.animateAlways){
            let layerAnimation = CABasicAnimation(keyPath: "shadowRadius")
            layerAnimation.fromValue = minGlowSize
            layerAnimation.toValue = maxGlowSize
            layerAnimation.isAdditive = false
            layerAnimation.duration = CFTimeInterval(animDuration/2)
            layerAnimation.fillMode = CAMediaTimingFillMode.forwards
            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.animateAlways){
            let layerAnimation = CABasicAnimation(keyPath: "shadowRadius")
            layerAnimation.fromValue = maxGlowSize
            layerAnimation.toValue = minGlowSize
            layerAnimation.isAdditive = false
            layerAnimation.duration = CFTimeInterval(animDuration/2)
            layerAnimation.fillMode = CAMediaTimingFillMode.forwards
            layerAnimation.isRemovedOnCompletion = false
            self.layer.add(layerAnimation, forKey: "removeGlowing")
        }
    }

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

        if(!self.animateAlways){
            let layerAnimation = CABasicAnimation(keyPath: "shadowRadius")
            layerAnimation.fromValue = maxGlowSize
            layerAnimation.toValue = minGlowSize
            layerAnimation.isAdditive = false
            layerAnimation.duration = CFTimeInterval(animDuration/2)
            layerAnimation.fillMode = CAMediaTimingFillMode.forwards
            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 = CAMediaTimingFillMode.forwards
        layerAnimation.isRemovedOnCompletion = false
        layerAnimation.repeatCount = .infinity
        self.layer.add(layerAnimation, forKey: "glowingAnimation")

    }

    func stopAnimations() {
        self.layer.removeAllAnimations()
    }

}

enter image description here

Edited: Added Inspectable attributes for better customization

Edited: Adapted to swift4 and added func to stop animation

like image 157
Reinier Melian Avatar answered Dec 11 '22 09:12

Reinier Melian