上面提到一个朦胧遮罩的实现方式
接下来介绍另外一种实现,利用iOS7所提供的 UIVisualEffectView 来实现(自行百度一下这个类的介绍)
并且前提先要了解清楚:
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event;
hitTest这个方法结合UIWindow,经常来配合使用,比较多的场景有置顶操作,朦胧遮罩层的实现等等
最重要的功能是这个方法可以穿透视图层,这样你可以锁定你要响应的对象,这里记住这个话,以后大大有所用
一、定义UIWindow类,作为响应罩面:
#import <UIKit/UIKit.h>
@interface OverheadView : UIWindow
@end
实现:
#import "OverheadView.h"
@implementation OverheadView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
id obj = [super hitTest:point withEvent:event];
if ([obj isKindOfClass:NSClassFromString(@"HyRoundMenuView")]) {
return obj;
}
return nil;
}
@end
稍微解释一下:出现朦胧遮罩面时候,返回obj响应对象,否则,穿透该window层级(无视它)
二、定义遮罩层视图
#import <UIKit/UIKit.h>
#import "HyRoundMenuModel.h"
typedef enum : NSUInteger
{
HyRoundMenuViewBackgroundViewTypeBlur NS_ENUM_AVAILABLE_IOS(9_0) = 0,
HyRoundMenuViewBackgroundViewTypeCustomColors = 1,
} HyRoundMenuViewBackgroundViewType;
@interface HyRoundMenuView : UIControl
@property (nonatomic, assign) UIBlurEffectStyle blurEffectStyle;
@property (nonatomic, nonnull, copy ) UIColor *customBackgroundViewColor;
@property (nonatomic, assign) HyRoundMenuViewBackgroundViewType backgroundViewType;
+ (__nonnull instancetype) shareInstance;
@end
#import "HyRoundMenuView.h"
#import "OverheadView.h"
#define IH_DEVICE_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define IH_DEVICE_WIDTH [[UIScreen mainScreen] bounds].size.width
@interface HyRoundMenuView ()
@property (nonatomic, nonnull, strong) UIView *backgroundView;
@property (nonatomic, nonnull, strong) OverheadView *topView;
@end
@implementation HyRoundMenuView
static HyRoundMenuView* _instance = nil;
+(instancetype) shareInstance
{
static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
_instance = [[super allocWithZone:NULL] init] ;
}) ;
return _instance ;
}
- (instancetype) init
{
self = [super init];
if (self) { [self initUI]; }
return self;
}
- (void)initUI
{
self.frame = [UIScreen mainScreen].bounds;
self.backgroundColor = [UIColor clearColor];
self.userInteractionEnabled = true;
_blurEffectStyle = UIBlurEffectStyleLight;
_backgroundViewType = HyRoundMenuViewBackgroundViewTypeBlur;
//backgroundView
if (!_backgroundView) {
_backgroundView = [[UIVisualEffectView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_backgroundView.hidden = YES;
_backgroundView.backgroundColor = [UIColor clearColor];
((UIVisualEffectView *)_backgroundView).effect = nil;
}
[_backgroundView setFrame:self.bounds];
[self addSuperView];
}
- (void)addSuperView
{
_topView = [[OverheadView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_topView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
_topView.windowLevel = UIWindowLevelAlert;
_topView.hidden = NO;
_topView.alpha = 1;
[_topView makeKeyAndVisible];
UIViewController *vc = [UIViewController new];
_topView.rootViewController = vc;
[_topView addSubview:_backgroundView];
[_topView addSubview:self];
}
- (void)startBackgroundViewAnimationIsOpen:(BOOL)isMasking
{
if (isMasking)
{
((UIVisualEffectView *)_backgroundView).hidden = (_backgroundViewType == HyRoundMenuViewBackgroundViewTypeCustomColors);
[UIView animateWithDuration:0.3f animations:^{//customBackgroundViewColor
if (_backgroundViewType != HyRoundMenuViewBackgroundViewTypeCustomColors) ((UIVisualEffectView *)_backgroundView).effect = [UIBlurEffect effectWithStyle:_blurEffectStyle];
if (_backgroundViewType == HyRoundMenuViewBackgroundViewTypeCustomColors) self.backgroundColor = _customBackgroundViewColor;
}];
}
else
{
[UIView animateWithDuration:0.3f animations:^{
if (_backgroundViewType != HyRoundMenuViewBackgroundViewTypeCustomColors) ((UIVisualEffectView *)_backgroundView).effect = nil;
if (_backgroundViewType == HyRoundMenuViewBackgroundViewTypeCustomColors) self.backgroundColor = [UIColor clearColor];
} completion:^(BOOL finished) {
_backgroundView.hidden = _backgroundViewType == HyRoundMenuViewBackgroundViewTypeCustomColors;
}];
}
}
static BOOL isMasking = false;
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
isMasking = !isMasking;
[self startBackgroundViewAnimationIsOpen:isMasking];
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
id obj = [super hitTest:point withEvent:event];
return obj;
}
@end
具体实现,也没什么好说的,千万记得hitTest在这里面的功用就行了,其实遮罩层的hitTest方法并没有达到什么作用,但是为什么保留呢?
因为这个方法在“过滤”的效果非常好用,以后在别的需求会讲到这个方法的作用
实现是转载学习于:
https://github.com/wwdc14/HyRoundMenuView