ios - buttons - uicontrol swift
AƱadiendo efecto de brillo a UIButton-iOS (5)
Tengo un UIButton que es un logo. Este botón con el logotipo se iluminará para siempre, pero dejará de brillar al tocarlo. Es como una animación brillante.
¿Hay alguna sugerencia?
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_CABasicAnimation", referenced from:
objc-class-ref in UIView+Glow.o
"_OBJC_CLASS_$_CAMediaTimingFunction", referenced from:
objc-class-ref in UIView+Glow.o
"_kCAMediaTimingFunctionEaseInEaseOut", referenced from:
-[UIView(Glow) startGlowing] in UIView+Glow.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Aquí está mi respuesta ...
Utilizando categorías
/* ****FAQ?
1.how to add glow effect on uibutton?
[UIButton glowUIButton:playButton];
2.how to remove effect on uibutton?
[UIButton removeGlowUIButton:playButton];
Ends*** */
#import <UIKit/UIKit.h>
@interface UIButton (BlinkEffect)
//blink effect category
+( void) glowUIButton:(UIButton *)inputButton;
//remove blink effect
+(void) removeGlowUIButton:(UIButton *)inputButton;
@end
#import "UIButton+BlinkEffect.h"
@implementation UIButton (BlinkEffect)
+(void) glowUIButton:(UIButton *)inputButton
{
//add blink effect
CALayer *viewLayer = inputButton.layer;
viewLayer.shadowOffset = CGSizeMake(0,0);
CGFloat radius = CGRectGetWidth(inputButton.bounds)/2.0;
viewLayer.shadowColor = [[UIColor whiteColor] CGColor];
viewLayer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-7,-5.5, 2.5 * (radius), 2.5 * radius) cornerRadius:radius].CGPath;
viewLayer.shadowRadius = 5.0f;
viewLayer.shadowOpacity = 1.0f;
//Let''s animate it while we''re at it.
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
animation.duration =0.7;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.fromValue = [NSNumber numberWithFloat:1.0];
animation.toValue = [NSNumber numberWithFloat:0.0];
animation.autoreverses = YES;
animation.repeatCount = 1.0 / 0.0;
[viewLayer addAnimation:animation forKey:@"shadowOpacity"];
}
+(void)removeGlowUIButton:(UIButton *)inputButton
{
CALayer *viewLayer = inputButton.layer;
viewLayer.shadowColor = [[UIColor clearColor] CGColor];
[viewLayer removeAnimationForKey:@"shadowOpacity"];
}
@end
Código brillante tomado de: Creación de un efecto de brillo para UILabel y UIButton
Primero, necesitarás importar el Framework QuartzCore:
#import <QuartzCore/QuartzCore.h>
Cuando creas un botón (o en viewDidLoad
, depende de tu estructura de código) agrega este código:
UIColor *color = button.currentTitleColor;
button.titleLabel.layer.shadowColor = [color CGColor];
button.titleLabel.layer.shadowRadius = 4.0f;
button.titleLabel.layer.shadowOpacity = .9;
button.titleLabel.layer.shadowOffset = CGSizeZero;
button.titleLabel.layer.masksToBounds = NO;
Tendrá que estar atento a dos eventos: UIControlEventTouchDown
y UIControlEventTouchUpInside
En el controlador UIControlEventTouchDown
, agregará el código:
UIColor *color = [UIColor clearColor];
button.titleLabel.layer.shadowColor = [color CGColor];
Y en el controlador UIControlEventUpInside
agregará el código:
UIColor *color = button.currentTitleColor;
button.titleLabel.layer.shadowColor = [color CGColor];
Una vez más, los detalles de la implementación dependen de si creas botones de programación o mediante Interface Builder, pero estoy seguro de que podrás resolverlo a partir de ahora.
EDITAR: para un botón personalizado, simplemente agregando el siguiente código debería funcionar:
[button setImage:[UIImage imageNamed:@"buttonWithGlow.png"]
forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"buttonWithNoGlow.png"]
forState:UIControlStateHighlighted];
Me gusta esta animación de glow + grow / shrink para mis botones "especiales":
-(void)makeViewShine:(UIView*) view
{
view.layer.shadowColor = [UIColor yellowColor].CGColor;
view.layer.shadowRadius = 10.0f;
view.layer.shadowOpacity = 1.0f;
view.layer.shadowOffset = CGSizeZero;
[UIView animateWithDuration:0.7f delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction animations:^{
[UIView setAnimationRepeatCount:15];
view.transform = CGAffineTransformMakeScale(1.2f, 1.2f);
} completion:^(BOOL finished) {
view.layer.shadowRadius = 0.0f;
view.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
}];
}
Swift 4
1- Crear extensión UIView con la animación.
2- Úselo en campos de texto, botones, vistas (cualquier subclase de UIView)
Nota: puede cambiar los valores y jugar para obtener el efecto que necesita.
Extensión UIView
import UIKit
extension UIView {
enum GlowEffect: Float {
case small = 0.4, normal = 2, big = 15
}
func doGlowAnimation(withColor color: UIColor, withEffect effect: GlowEffect = .normal) {
layer.masksToBounds = false
layer.shadowColor = color.cgColor
layer.shadowRadius = 0
layer.shadowOpacity = 1
layer.shadowOffset = .zero
let glowAnimation = CABasicAnimation(keyPath: "shadowRadius")
glowAnimation.fromValue = 0
glowAnimation.toValue = effect.rawValue
glowAnimation.beginTime = CACurrentMediaTime()+0.3
glowAnimation.duration = CFTimeInterval(0.3)
glowAnimation.fillMode = kCAFillModeRemoved
glowAnimation.autoreverses = true
glowAnimation.isRemovedOnCompletion = true
layer.add(glowAnimation, forKey: "shadowGlowingAnimation")
}
}
Cómo usarlo:
//TextField with border
textField.doGlowAnimation(withColor: UIColor.red, withEffect: .big)
//Label
label.doGlowAnimation(withColor: label.textColor, withEffect: .small)
//Button
button.doGlowAnimation(withColor: UIColor.red, withEffect: .big)
Te sugiero que uses la categoría de resplandor de UIView hecha por el laboratorio secreto .
El ejemplo está disponible here