Adding Glow Effect to Uibutton - iOS

Adding glow effect to UIButton - iOS

I suggest you use the Glow Category of UIView made by secret lab.

Example is available here

Glow effect to custom UIButton like info UIButton

I found an easy way to do as I want.
It's by enabling check mark of "Shows Touch On Highlight" property.
See the screenshot..

Sample Image

It's working fine for my custom buttons.

how to create UIButton shadow glow effect?

_closeButton.imageView.layer.cornerRadius = 7.0f; 
_closeButton.layer.shadowRadius = 3.0f;
_closeButton.layer.shadowColor = [UIColor blackColor].CGColor;
_closeButton.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
_closeButton.layer.shadowOpacity = 0.5f;
_closeButton.layer.masksToBounds = NO;

check this

Need a Glowing Animation around a button

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()
}

}

Sample Image

Edited: Added Inspectable attributes for better customization

Edited: Adapted to swift4 and added func to stop animation

How can I add the 'Glow effect' to UIBarButtonItem?

You mean the effect you get when you touch an button? That is a property on an UIButton (also accessible from Interface Builder);

@property(nonatomic) BOOL showsTouchWhenHighlighted

So...

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.showsTouchWhenHighlighted = YES;

If you write a subclass of UIBarButtonItem and put this code in there somewhere it is really easy (and not messy) to re-use it. I'm not sure if there is any other way.

UIButton glow effect

You have to do this yourself. Load a glow png into a UIImageView and add it into the view hierarchy. Then set the view's position based on touches.

http://i.imgur.com/RS2kQ.png



Related Topics



Leave a reply



Submit