1、基本使用
FGPopupScheduler *Scheduler = FGPopupSchedulerGetForPSS(FGPopupSchedulerStrategyFIFO);
AnimationShowPopupView *pop1 = [[AnimationShowPopupView alloc] initWithDescrption:@"自定义动画 pop2" scheduler:Scheduler];
ConditionsPopView *pop2 = [[ConditionsPopView alloc] initWithDescrption:@"条件弹窗 pop3 Discard" scheduler:Scheduler];
[Scheduler add:pop];
[Scheduler add:pop2];
2、AnimationShowPopupView、ConditionsPopView都是需要实现FGPopupView protocol的NSObject(一般是一个UIView的类)
FGPopupView protocol中的方法都是optional可选的:
@protocol FGPopupView <NSObject>
@optional
/*
FGPopupSchedulerStrategyQueue会根据 -showPopupView: 做显示逻辑,如果含有动画请实现-showPopupViewWithAnimation:方法
*/
- (void)showPopupView;
/*
FGPopupSchedulerStrategyQueue会根据 -dismissPopupView: 做隐藏逻辑,如果含有动画请实现-showPopupViewWithAnimation:方法
*/
- (void)dismissPopupView;
/*
FGPopupSchedulerStrategyQueue会根据 -showPopupViewWithAnimation: 来做显示逻辑。如果block不传可能会出现意料外的问题
*/
- (void)showPopupViewWithAnimation:(FGPopupViewAnimationBlock)block;
/*
FGPopupSchedulerStrategyQueue会根据 -dismissPopupView: 做隐藏逻辑,如果含有动画请实现-dismissPopupViewWithAnimation:方法,如果block不传可能会出现意料外的问题
*/
- (void)dismissPopupViewWithAnimation:(FGPopupViewAnimationBlock)block;
/**
FGPopupSchedulerStrategyQueue会根据-canRegisterFirstPopupView判断,当队列顺序轮到它的时候是否能够成为响应的第一个优先级PopupView。默认为YES
*/
- (BOOL)canRegisterFirstPopupViewResponder;
/** 0.4.0 新增*/
/**
FGPopupSchedulerStrategyQueue 会根据 - popupViewUntriggeredBehavior:来决定触发时弹窗的显示行为,默认为 FGPopupViewUntriggeredBehaviorAwait
*/
- (FGPopupViewUntriggeredBehavior)popupViewUntriggeredBehavior;
/**
FGPopupViewSwitchBehavior 会根据 - popupViewSwitchBehavior:来决定已经显示的弹窗,是否会被后续更高优先级的弹窗锁影响,默认为 FGPopupViewSwitchBehaviorAwait ⚠️⚠️ 只在FGPopupSchedulerStrategyPriority生效
*/
- (FGPopupViewSwitchBehavior)popupViewSwitchBehavior;
@end
3、调度策略
typedef NS_ENUM(NSUInteger, FGPopupSchedulerStrategy) {
FGPopupSchedulerStrategyFIFO = 1 << 0, //先进先出
FGPopupSchedulerStrategyLIFO = 1 << 1, //后进先出
FGPopupSchedulerStrategyPriority = 1 << 2 //优先级调度
};
可以根据需求选择合适的策略,另外实际上还可以结合 FGPopupSchedulerStrategyPriority | FGPopupSchedulerStrategyFIFO 一起使用,来处理当选择优先级策略时,如何决定同一优先级弹窗的排序。
4、触发策略
用户可以根据它来决定,当弹窗触发显示逻辑时是否要继续等待
typedef NS_ENUM(NSUInteger, FGPopupViewUntriggeredBehavior) {
FGPopupViewUntriggeredBehaviorDiscard, //未满足条件时会被直接丢弃
FGPopupViewUntriggeredBehaviorAwait, //未满足条件时会继续等待
};
5、添加策略
在使用FGPopupSchedulerStrategyPriority时碰到优先级相同时,应该如何添加,这里有两个选择,一个先进先出,一个先进后出
typedef NS_ENUM(NSUInteger, FGPopupPriorityAddStrategy) {
FGPopupPriorityAddStrategyFIFO, //FIFO
FGPopupPriorityAddStrategyLIFO, //LIFO
};
6、切换策略
目前支持3种切换行为, 用户可以根据它来决定,当该弹窗已经显示时,是否会被后续高优线级的弹窗影响。仅在优先级调度策略时生效:FGPopupSchedulerStrategyPriority
typedef NS_ENUM(NSUInteger, FGPopupViewSwitchBehavior) {
FGPopupViewSwitchBehaviorDiscard, //当该弹窗已经显示,如果后面来了弹窗优先级更高的弹窗时,显示更高优先级弹窗并且当前弹窗会被抛弃
FGPopupViewSwitchBehaviorLatent, //当该弹窗已经显示,如果后面来了弹窗优先级更高的弹窗时,显示更高优先级弹窗并且当前弹窗重新进入队列, PS:优先级相同时同 FGPopupViewSwitchBehaviorDiscard
FGPopupViewSwitchBehaviorAwait, //当该弹窗已经显示时,不会被后续高优线级的弹窗影响
};
7、核心逻辑
//类FGPopupScheduler
//注册observer 通过Runloop监听主线程空闲的时刻
+ (void)initialize{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
CFRunLoopObserverRef observer = CFRunLoopObserverCreate(CFAllocatorGetDefault(), kCFRunLoopBeforeWaiting | kCFRunLoopExit, true, 0xFFFFFF, FGRunLoopObserverCallBack, nil);
CFRunLoopAddObserver(CFRunLoopGetMain(), observer, kCFRunLoopCommonModes);
CFRelease(observer);
});
}
/*
以下三个方法调用了registerFirstPopupViewResponder
*/
- (void)setSuspended:(BOOL)suspended{
dispatch_async_main_safe(^(){
self->_suspended = suspended;
if (!suspended) [self registerFirstPopupViewResponder];
});
}
//添加弹窗对象的时候
- (void)add:(id<FGPopupView>)view Priority:(FGPopupStrategyPriority)Priority{
dispatch_async_main_safe(^(){
[self->_list addPopupView:view Priority:Priority];
[self registerFirstPopupViewResponder];
});
}
//通过Runloop监听主线程空闲的时刻
static void FGRunLoopObserverCallBack(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info){
for (FGPopupScheduler *scheduler in FGPopupSchedulers()) {
if (![scheduler isEmpty]) {
[scheduler registerFirstPopupViewResponder];
}
}
}
//类FGPopupScheduler
/**
向调度器主动发送一个执行显示弹窗的命令, 支持线程安全
*/
- (void)registerFirstPopupViewResponder{
if (!self.suspended && self.canRegisterFirstPopupViewResponder) {
dispatch_async_main_safe(^(){
[self->_list execute];
});
}
}
//类FGPopupScheduler
/**
返回当前调度器是否拥有已经显示的弹窗, 如果canRegisterFirstPopupViewResponder为true,-registerFirstPopupViewResponder将执行无效
*/
@property (nonatomic, assign, readonly) BOOL canRegisterFirstPopupViewResponder;
//类FGPopupList : NSObject <FGPopupSchedulerStrategyQueue>
//FGPopupSchedulerStrategyQueue可在第8点查看
/*这里有个疑问,这里传进来的priority并没有存储也没有使用不会影响功能?
答案是不会,因为FGPopupList是基类,其子类FGPopupPriorityList对这个方法进行了重写,
这也对应了其仅在优先级调度策略时生效:FGPopupSchedulerStrategyPriority
*/
- (void)addPopupView:(id<FGPopupView>)view Priority:(FGPopupStrategyPriority)Priority{
[self monitorRemoveEventWith:view];
}
//类FGPopupList : NSObject <FGPopupSchedulerStrategyQueue>
- (void)execute{
PopupElement *elemt = [self _hitTestFirstPopupResponder];
id<FGPopupView> view = elemt.data;
if (!view) {
return;
}
self.FirstFirstResponderElement = elemt;
if ([view respondsToSelector:@selector(showPopupViewWithAnimation:)]) {
[view showPopupViewWithAnimation:^{}];
}
else if([view respondsToSelector:@selector(showPopupView)]){
[view showPopupView];
}else{
NSAssert(NO, @"You must have to implementation -showPopupViewWithAnimation: or -showPopupView");
}
}
//类FGPopupList : NSObject <FGPopupSchedulerStrategyQueue>
/*
进行第一响应者测试并返回对应的节点
@returns 作为第一响应者的节点
*/
- (PopupElement *)_hitTestFirstPopupResponder{
PopupElement *element;
for(auto itor=_list.begin(); itor!=_list.end();) {
PopupElement *temp = *itor;
id<FGPopupView> data = temp.data;
__block BOOL canRegisterFirstPopupViewResponder = YES;
if ([data respondsToSelector:@selector(canRegisterFirstPopupViewResponder)]) {
canRegisterFirstPopupViewResponder = [data canRegisterFirstPopupViewResponder];
}
if (canRegisterFirstPopupViewResponder) {
element = temp;
break;
}
/// 这里只能由为显示的popup所触发
else if([data respondsToSelector:@selector(popupViewUntriggeredBehavior)] && [data popupViewUntriggeredBehavior] == FGPopupViewUntriggeredBehaviorDiscard){
itor = _list.erase(itor++);
}
else{
itor++;
}
}
return element;
}
8、FGPopupSchedulerStrategyQueue
@protocol FGPopupSchedulerStrategyQueue <NSObject>
/**
向当前队列中添加弹窗对象,根据不同的FGPopupSchedulerStrategy,每个subList自己都需要重构的-addPopupView:方法
@param view 弹窗对象
@param Priority 优先级
*/
- (void)addPopupView:(id<FGPopupView>)view Priority:(FGPopupStrategyPriority)Priority;
/**
从当前队列删除指定的弹窗对象,根据不同的FGPopupSchedulerStrategy,每个subList自己都需要重构的-removePopupView:方法
*/
- (void)removePopupView:(id<FGPopupView>)view;
/**
从当前队列中进行-hitTest,返回对象作为当前的FirstFirstResponder,并执行显示操作
*/
- (void)execute;
/**
清除当前队列弹窗,
*/
- (void)clear;
/**
返回当前队列是否存在弹窗
*/
- (BOOL)isEmpty;
/**
返回是否能注册新的显示弹窗,如果当前已经有显示的弹窗返回NO
*/
- (BOOL)canRegisterFirstFirstPopupViewResponder;
@end
网友评论