UIView+Glow.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // UIView+Glow.m
  3. //
  4. //
  5. // Created by James on 11/4/14.
  6. // Copyright (c) 2014 James. All rights reserved.
  7. //
  8. #import "UIView+Glow.h"
  9. #import <QuartzCore/QuartzCore.h>
  10. #import <objc/runtime.h>
  11. @implementation UIView (Glow)
  12. static char* GLOWVIEW_KEY = "GLOWVIEW";
  13. - (UIView *)glowView
  14. {
  15. return objc_getAssociatedObject(self, GLOWVIEW_KEY);
  16. }
  17. - (void)setGlowView:(UIView*)glowView
  18. {
  19. objc_setAssociatedObject(self, GLOWVIEW_KEY, glowView, OBJC_ASSOCIATION_RETAIN);
  20. }
  21. - (void)startGlowingWithColor:(UIColor *)color intensity:(CGFloat)intensity
  22. {
  23. [self startGlowingWithColor:color fromIntensity:0.1 toIntensity:intensity repeat:YES];
  24. }
  25. - (void)startGlowingWithColor:(UIColor*)color fromIntensity:(CGFloat)fromIntensity toIntensity:(CGFloat)toIntensity repeat:(BOOL)repeat
  26. {
  27. [self startGlowingWithColor:color fromIntensity:0.1 toIntensity:toIntensity repeat:YES duration:1];
  28. }
  29. - (void)startGlowingWithColor:(UIColor*)color fromIntensity:(CGFloat)fromIntensity toIntensity:(CGFloat)toIntensity repeat:(BOOL)repeat duration:(CGFloat)dur
  30. {
  31. if ([self glowView])
  32. {
  33. return;
  34. }
  35. UIImage* image;
  36. UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
  37. [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  38. UIBezierPath* path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
  39. [color setFill];
  40. [path fillWithBlendMode:kCGBlendModeSourceAtop alpha:1.0];
  41. image = UIGraphicsGetImageFromCurrentImageContext();
  42. UIGraphicsEndImageContext();
  43. UIView *glowView = [[UIImageView alloc] initWithImage:image];
  44. [self.superview insertSubview:glowView aboveSubview:self];
  45. glowView.center = self.center;
  46. glowView.alpha = 0;
  47. glowView.layer.shadowColor = color.CGColor;
  48. glowView.layer.shadowOffset = CGSizeZero;
  49. glowView.layer.shadowRadius = 10;
  50. glowView.layer.shadowOpacity = 1.0;
  51. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
  52. animation.fromValue = @(fromIntensity);
  53. animation.toValue = @(toIntensity);
  54. animation.repeatCount = repeat ? HUGE_VAL : 0;
  55. animation.duration = dur;
  56. animation.autoreverses = YES;
  57. animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  58. [glowView.layer addAnimation:animation forKey:@"pulse"];
  59. [self setGlowView:glowView];
  60. }
  61. - (void)glowOnceAtLocation:(CGPoint)point inView:(UIView *)view
  62. {
  63. [self startGlowingWithColor:[UIColor whiteColor] fromIntensity:0 toIntensity:0.6 repeat:NO];
  64. [self glowView].center = point;
  65. [view addSubview:[self glowView]];
  66. int64_t delayInSeconds = 2.0;
  67. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
  68. dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  69. [self stopGlowing];
  70. });
  71. }
  72. - (void)glowOnce
  73. {
  74. [self startGlowing];
  75. int64_t delayInSeconds = 2.0;
  76. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
  77. dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  78. [self stopGlowing];
  79. });
  80. }
  81. - (void)startGlowing
  82. {
  83. [self startGlowingWithColor:[UIColor whiteColor] intensity:0.6];
  84. }
  85. - (void)stopGlowing
  86. {
  87. [[self glowView] removeFromSuperview];
  88. [self setGlowView:nil];
  89. }
  90. @end