redribben

buttonSubclass

Jun 18th, 2015
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #import "BMButton.h"
  3. #import "BMIconCode.h"
  4. #import "BMUIApplication.h"
  5. #import "BMButtonIdentification.h"
  6.  
  7.  
  8. @implementation BMButton
  9.  
  10. @synthesize visualEffectView;
  11. @synthesize initialStyledIcon;
  12. @synthesize selectedStyledIcon;
  13. //@synthesize maskLayerNotSelect;
  14. //@synthesize maskLayerSelect;
  15.  
  16. -(id)initWithCoder:(NSCoder *)aDecoder {
  17.     self = [super initWithCoder:aDecoder];
  18.     if(self){
  19.         [self initialize];
  20.     }
  21.     return self;
  22. }
  23.  
  24. - (void)initialize {
  25.     CGRect rect = self.bounds;
  26.    
  27.     initialStyledIcon = [[BMButtonIdentification alloc] initialImageWithRestorationIdentification:self.restorationIdentifier];
  28.     selectedStyledIcon = [[BMButtonIdentification alloc] selectedImageWithRestorationIdentification:self.restorationIdentifier];
  29.    
  30.     self.autoresizesSubviews = YES;
  31.    
  32.     CALayer* maskLayer = [CALayer layer];
  33.     maskLayer.frame = rect;
  34.     maskLayer.contents = (__bridge id)[initialStyledIcon CGImage];
  35.    
  36.     //Add Blurring Effect
  37.     UIVisualEffect *blurEffect;
  38.     blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  39.     UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
  40.     visualEffectView = [[UIVisualEffectView alloc] initWithEffect:effect];
  41.     [self.visualEffectView effect];
  42.     self.visualEffectView.layer.mask = maskLayer;
  43.     [self addSubview:visualEffectView];
  44.     visualEffectView.userInteractionEnabled = NO;
  45.     //    visualEffectView.exclusiveTouch = NO;
  46.    
  47.     [[NSNotificationCenter defaultCenter] addObserver:self
  48.                                              selector:@selector(backFromIdle)
  49.                                                  name:@"AppActive"
  50.                                                object:nil];
  51.     [[NSNotificationCenter defaultCenter] addObserver:self
  52.                                              selector:@selector(idleTimeKickedIn)
  53.                                                  name:kApplicationDidTimeoutNotification
  54.                                                object:nil];
  55. }
  56.  
  57.  
  58. - (void)idleTimeKickedIn {
  59.     [self refreshBlurViewWithStatus:3];
  60. }
  61.  
  62.  
  63. - (void)backFromIdle {
  64.     if (self.highlighted) {
  65.         [self refreshBlurViewWithStatus:1];
  66.     }
  67.     else {
  68.         [self refreshBlurViewWithStatus:0];
  69.     }
  70. }
  71.  
  72.  
  73. - (void)refreshBlurViewWithStatus:(int)status {
  74.    
  75.     [UIView transitionWithView:self.visualEffectView.superview
  76.                       duration:.25
  77.                        options:UIViewAnimationOptionTransitionCrossDissolve
  78.                     animations:^{
  79.                         [visualEffectView removeFromSuperview];
  80.                     }
  81.                     completion:nil];
  82.    
  83.     visualEffectView = nil;
  84.    
  85.     CGRect rect = self.bounds;
  86.    
  87.     CALayer* maskLayer = [CALayer layer];
  88.     maskLayer.frame = rect;
  89.     if (self.highlighted == TRUE) {
  90.         maskLayer.contents = (__bridge id)[selectedStyledIcon CGImage];
  91.     }
  92.     else {
  93.         maskLayer.contents = (__bridge id)[initialStyledIcon CGImage];
  94.     }
  95.    
  96.     UIVisualEffect *blurEffect;
  97.     switch (status) {
  98.         case 0:
  99.             blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
  100.             break;
  101.         case 1:
  102.             blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
  103.             break;
  104.         case 3:
  105.             blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  106.             break;
  107.         default:
  108.             break;
  109.     }
  110.     //        UIVisualEffectView *visualEffectView;
  111.     visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
  112.     visualEffectView.frame = self.bounds;
  113.     visualEffectView.layer.mask = maskLayer;
  114.     [self addSubview:visualEffectView];
  115.     visualEffectView.userInteractionEnabled = NO;
  116.     //    visualEffectView.exclusiveTouch = NO;
  117.     [UIView commitAnimations];
  118. }
  119.  
  120.  
  121. - (void)layoutSubviews {
  122.     [super layoutSubviews];
  123. //    maskLayerNotSelect.frame = self.bounds;
  124. //    maskLayerSelect.frame = self.bounds;
  125.     visualEffectView.frame = self.bounds;
  126.     //    [self setNeedsDisplay];
  127. }
  128.  
  129.  
  130. -(void)setHighlighted:(BOOL)highlighted {
  131.     [super setHighlighted:highlighted];
  132.     [self refreshBlurViewWithStatus:highlighted];
  133. }
  134.  
  135. -(void)setSelected:(BOOL)selected {
  136.     [super setSelected:selected];
  137.    
  138.    
  139. }
  140. @end
Advertisement
Add Comment
Please, Sign In to add comment