2015-10-05 20:41:32 +00:00
|
|
|
#import "BlurView.h"
|
2017-04-15 09:55:46 +00:00
|
|
|
#import "BlurEffectWithAmount.h"
|
2016-10-27 07:19:43 +00:00
|
|
|
|
2017-04-10 15:18:39 +00:00
|
|
|
@interface BlurView ()
|
2015-10-05 20:41:32 +00:00
|
|
|
|
2017-04-10 15:18:39 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation BlurView
|
|
|
|
|
2019-08-21 06:19:31 +00:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
if (self = [super init]) {
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(reduceTransparencyStatusDidChange:)
|
|
|
|
name:UIAccessibilityReduceTransparencyStatusDidChangeNotification
|
|
|
|
object:nil];
|
|
|
|
}
|
2017-04-15 09:55:46 +00:00
|
|
|
|
2019-08-21 06:19:31 +00:00
|
|
|
return self;
|
|
|
|
}
|
2017-04-10 15:18:39 +00:00
|
|
|
|
2019-08-21 06:19:31 +00:00
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
if (self = [super initWithFrame:frame]) {
|
|
|
|
self.blurEffectView = [[UIVisualEffectView alloc] init];
|
|
|
|
self.blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
|
|
self.blurEffectView.frame = frame;
|
2017-04-10 15:18:39 +00:00
|
|
|
|
2019-08-21 06:19:31 +00:00
|
|
|
self.reducedTransparencyFallbackView = [[UIView alloc] init];
|
|
|
|
self.reducedTransparencyFallbackView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
|
|
self.reducedTransparencyFallbackView.frame = frame;
|
2017-04-10 15:18:39 +00:00
|
|
|
|
2019-08-21 06:19:31 +00:00
|
|
|
self.blurAmount = @10;
|
|
|
|
self.blurType = @"dark";
|
|
|
|
[self updateBlurEffect];
|
|
|
|
[self updateFallbackView];
|
|
|
|
|
|
|
|
self.clipsToBounds = true;
|
|
|
|
|
|
|
|
[self addSubview:self.blurEffectView];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-10-05 20:41:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-15 09:55:46 +00:00
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
|
|
|
self.blurEffectView.frame = self.bounds;
|
2019-08-21 06:19:31 +00:00
|
|
|
self.reducedTransparencyFallbackView.frame = self.bounds;
|
2017-04-15 09:55:46 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 20:41:32 +00:00
|
|
|
- (void)setBlurType:(NSString *)blurType
|
|
|
|
{
|
2017-04-15 09:55:46 +00:00
|
|
|
if (blurType && ![self.blurType isEqual:blurType]) {
|
|
|
|
_blurType = blurType;
|
|
|
|
[self updateBlurEffect];
|
|
|
|
}
|
2015-10-05 20:41:32 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 07:19:43 +00:00
|
|
|
- (void)setBlurAmount:(NSNumber *)blurAmount
|
2015-10-05 20:41:32 +00:00
|
|
|
{
|
2017-04-15 09:55:46 +00:00
|
|
|
if (blurAmount && ![self.blurAmount isEqualToNumber:blurAmount]) {
|
|
|
|
_blurAmount = blurAmount;
|
|
|
|
[self updateBlurEffect];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 06:19:31 +00:00
|
|
|
- (void)setReducedTransparencyFallbackColor:(nullable UIColor *)reducedTransparencyFallbackColor
|
|
|
|
{
|
|
|
|
if (reducedTransparencyFallbackColor && ![self.reducedTransparencyFallbackColor isEqual:reducedTransparencyFallbackColor]) {
|
|
|
|
_reducedTransparencyFallbackColor = reducedTransparencyFallbackColor;
|
|
|
|
[self updateFallbackView];
|
|
|
|
}
|
|
|
|
}
|
2017-04-15 09:55:46 +00:00
|
|
|
|
|
|
|
- (UIBlurEffectStyle)blurEffectStyle
|
|
|
|
{
|
|
|
|
if ([self.blurType isEqual: @"xlight"]) return UIBlurEffectStyleExtraLight;
|
|
|
|
if ([self.blurType isEqual: @"light"]) return UIBlurEffectStyleLight;
|
|
|
|
if ([self.blurType isEqual: @"dark"]) return UIBlurEffectStyleDark;
|
2019-03-26 17:53:50 +00:00
|
|
|
|
|
|
|
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000 /* __IPHONE_10_0 */
|
|
|
|
if ([self.blurType isEqual: @"regular"]) return UIBlurEffectStyleRegular;
|
|
|
|
if ([self.blurType isEqual: @"prominent"]) return UIBlurEffectStyleProminent;
|
|
|
|
#endif
|
2020-01-18 08:47:17 +00:00
|
|
|
|
2020-01-18 09:04:58 +00:00
|
|
|
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* __IPHONE_13_0 */
|
2020-01-18 08:47:17 +00:00
|
|
|
// Adaptable blur styles
|
2020-01-18 09:26:11 +00:00
|
|
|
if ([self.blurType isEqual: @"chromeMaterial"]) return UIBlurEffectStyleSystemChromeMaterial;
|
2020-01-18 08:47:17 +00:00
|
|
|
if ([self.blurType isEqual: @"material"]) return UIBlurEffectStyleSystemMaterial;
|
|
|
|
if ([self.blurType isEqual: @"thickMaterial"]) return UIBlurEffectStyleSystemThickMaterial;
|
2020-01-18 09:26:11 +00:00
|
|
|
if ([self.blurType isEqual: @"thinMaterial"]) return UIBlurEffectStyleSystemThinMaterial;
|
2020-01-18 08:47:17 +00:00
|
|
|
if ([self.blurType isEqual: @"ultraThinMaterial"]) return UIBlurEffectStyleSystemUltraThinMaterial;
|
|
|
|
// dark blur styles
|
|
|
|
if ([self.blurType isEqual: @"chromeMaterialDark"]) return UIBlurEffectStyleSystemChromeMaterialDark;
|
|
|
|
if ([self.blurType isEqual: @"materialDark"]) return UIBlurEffectStyleSystemMaterialDark;
|
|
|
|
if ([self.blurType isEqual: @"thickMaterialDark"]) return UIBlurEffectStyleSystemThickMaterialDark;
|
2020-01-18 09:26:11 +00:00
|
|
|
if ([self.blurType isEqual: @"thinMaterialDark"]) return UIBlurEffectStyleSystemThinMaterialDark;
|
2020-01-18 08:47:17 +00:00
|
|
|
if ([self.blurType isEqual: @"ultraThinMaterialDark"]) return UIBlurEffectStyleSystemUltraThinMaterialDark;
|
|
|
|
// light blur styles
|
|
|
|
if ([self.blurType isEqual: @"chromeMaterialLight"]) return UIBlurEffectStyleSystemChromeMaterialLight;
|
|
|
|
if ([self.blurType isEqual: @"materialLight"]) return UIBlurEffectStyleSystemMaterialLight;
|
|
|
|
if ([self.blurType isEqual: @"thickMaterialLight"]) return UIBlurEffectStyleSystemThickMaterialLight;
|
2020-01-18 09:26:11 +00:00
|
|
|
if ([self.blurType isEqual: @"thinMaterialLight"]) return UIBlurEffectStyleSystemThinMaterialLight;
|
2020-01-18 08:47:17 +00:00
|
|
|
if ([self.blurType isEqual: @"ultraThinMaterialLight"]) return UIBlurEffectStyleSystemUltraThinMaterialLight;
|
|
|
|
#endif
|
|
|
|
|
2017-06-27 13:50:55 +00:00
|
|
|
#if TARGET_OS_TV
|
2017-06-26 12:43:33 +00:00
|
|
|
if ([self.blurType isEqual: @"regular"]) return UIBlurEffectStyleRegular;
|
|
|
|
if ([self.blurType isEqual: @"prominent"]) return UIBlurEffectStyleProminent;
|
2019-03-26 17:53:50 +00:00
|
|
|
if ([self.blurType isEqual: @"extraDark"]) return UIBlurEffectStyleExtraDark;
|
2017-06-26 12:43:33 +00:00
|
|
|
#endif
|
2019-03-26 17:53:50 +00:00
|
|
|
|
2017-04-15 09:55:46 +00:00
|
|
|
return UIBlurEffectStyleDark;
|
|
|
|
}
|
|
|
|
|
2019-08-21 06:19:31 +00:00
|
|
|
- (BOOL)useReduceTransparencyFallback
|
|
|
|
{
|
|
|
|
return UIAccessibilityIsReduceTransparencyEnabled() == YES && self.reducedTransparencyFallbackColor != NULL;
|
|
|
|
}
|
|
|
|
|
2017-04-15 09:55:46 +00:00
|
|
|
- (void)updateBlurEffect
|
|
|
|
{
|
|
|
|
UIBlurEffectStyle style = [self blurEffectStyle];
|
|
|
|
self.blurEffect = [BlurEffectWithAmount effectWithStyle:style andBlurAmount:self.blurAmount];
|
|
|
|
self.blurEffectView.effect = self.blurEffect;
|
2016-10-27 07:19:43 +00:00
|
|
|
}
|
2015-10-05 20:41:32 +00:00
|
|
|
|
2019-08-21 06:19:31 +00:00
|
|
|
- (void)updateFallbackView
|
|
|
|
{
|
|
|
|
if ([self useReduceTransparencyFallback]) {
|
|
|
|
if (![self.subviews containsObject:self.reducedTransparencyFallbackView]) {
|
|
|
|
[self insertSubview:self.reducedTransparencyFallbackView atIndex:0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([self.subviews containsObject:self.blurEffectView]) {
|
|
|
|
[self.blurEffectView removeFromSuperview];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ([self.subviews containsObject:self.reducedTransparencyFallbackView]) {
|
|
|
|
[self.reducedTransparencyFallbackView removeFromSuperview];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (![self.subviews containsObject:self.blurEffectView]) {
|
|
|
|
[self insertSubview:self.blurEffectView atIndex:0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.reducedTransparencyFallbackView.backgroundColor = self.reducedTransparencyFallbackColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reduceTransparencyStatusDidChange:(__unused NSNotification *)notification
|
|
|
|
{
|
|
|
|
[self updateFallbackView];
|
|
|
|
}
|
|
|
|
|
2015-10-05 20:41:32 +00:00
|
|
|
@end
|