#import "BMButton.h" #import "BMIconCode.h" #import "BMUIApplication.h" #import "BMButtonIdentification.h" @implementation BMButton @synthesize visualEffectView; @synthesize initialStyledIcon; @synthesize selectedStyledIcon; //@synthesize maskLayerNotSelect; //@synthesize maskLayerSelect; -(id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if(self){ [self initialize]; } return self; } - (void)initialize { CGRect rect = self.bounds; initialStyledIcon = [[BMButtonIdentification alloc] initialImageWithRestorationIdentification:self.restorationIdentifier]; selectedStyledIcon = [[BMButtonIdentification alloc] selectedImageWithRestorationIdentification:self.restorationIdentifier]; self.autoresizesSubviews = YES; CALayer* maskLayer = [CALayer layer]; maskLayer.frame = rect; maskLayer.contents = (__bridge id)[initialStyledIcon CGImage]; //Add Blurring Effect UIVisualEffect *blurEffect; blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]; visualEffectView = [[UIVisualEffectView alloc] initWithEffect:effect]; [self.visualEffectView effect]; self.visualEffectView.layer.mask = maskLayer; [self addSubview:visualEffectView]; visualEffectView.userInteractionEnabled = NO; // visualEffectView.exclusiveTouch = NO; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backFromIdle) name:@"AppActive" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(idleTimeKickedIn) name:kApplicationDidTimeoutNotification object:nil]; } - (void)idleTimeKickedIn { [self refreshBlurViewWithStatus:3]; } - (void)backFromIdle { if (self.highlighted) { [self refreshBlurViewWithStatus:1]; } else { [self refreshBlurViewWithStatus:0]; } } - (void)refreshBlurViewWithStatus:(int)status { [UIView transitionWithView:self.visualEffectView.superview duration:.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ [visualEffectView removeFromSuperview]; } completion:nil]; visualEffectView = nil; CGRect rect = self.bounds; CALayer* maskLayer = [CALayer layer]; maskLayer.frame = rect; if (self.highlighted == TRUE) { maskLayer.contents = (__bridge id)[selectedStyledIcon CGImage]; } else { maskLayer.contents = (__bridge id)[initialStyledIcon CGImage]; } UIVisualEffect *blurEffect; switch (status) { case 0: blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]; break; case 1: blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; break; case 3: blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; break; default: break; } // UIVisualEffectView *visualEffectView; visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect]; visualEffectView.frame = self.bounds; visualEffectView.layer.mask = maskLayer; [self addSubview:visualEffectView]; visualEffectView.userInteractionEnabled = NO; // visualEffectView.exclusiveTouch = NO; [UIView commitAnimations]; } - (void)layoutSubviews { [super layoutSubviews]; // maskLayerNotSelect.frame = self.bounds; // maskLayerSelect.frame = self.bounds; visualEffectView.frame = self.bounds; // [self setNeedsDisplay]; } -(void)setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; [self refreshBlurViewWithStatus:highlighted]; } -(void)setSelected:(BOOL)selected { [super setSelected:selected]; } @end