美文网首页
iOS事件有序处理,比如首页弹窗

iOS事件有序处理,比如首页弹窗

作者: 七月不下雨 | 来源:发表于2019-12-17 17:35 被阅读0次

//效果


node.gif

//今天看到一篇文章试着写了下。https://blog.csdn.net/u014626094/article/details/101697305

#import <Foundation/Foundation.h>

typedef void (^Block)(NSInteger note_id) ;
NS_ASSUME_NONNULL_BEGIN
@protocol Node <NSObject>

- (NSInteger)getId;

- (void)onCompleted:(NSInteger)note_id;

@end

@interface WorkNode : NSObject<Node>

@property (nonatomic, assign) BOOL isWorking; //!<


@property (nonatomic, strong) id data;

@property (nonatomic, assign) NSInteger nodeID; //!<

//@property (nonatomic, strong) Work *work; //!<

/**
* 当前任务完成
*/
@property (nonatomic, copy) Block onWorkCompleted; //!<

//+(WorkNode *)BuildNodeId:(NSInteger)nodeId WithWork:(Work *)work;

- (instancetype)initNodeId:(NSInteger)nodeId WithWorkHandle:(void(^)(id <Node>))block;

- (void)doWork:(Block)block;

@end


@interface MKWorkFlowManager : NSObject
- (instancetype)initWithWorks:(NSMutableArray *)works ;
- (void)addNode:(id<Node>)node;


- (void)startWorking;

- (void)pauseExecuteAction;

- (void)startExecuteAction:(NSInteger)node_id;
@end

NS_ASSUME_NONNULL_END

////////////////////////////////////////////


#import "MKWorkFlowManager.h"

@interface WorkNode ()
@property (nonatomic, copy) void (^workBlock)(id <Node>); //!<
@end
@implementation WorkNode

//+(WorkNode *)BuildNodeId:(NSInteger)nodeId WithWork:(Work *)work {
//    return [[WorkNode alloc] initNodeId:nodeId WithWork:work];
//}
//
//- (instancetype)initNodeId:(NSInteger)nodeId WithWork:(Work *)work {
//    if (self = [super init]) {
//
//        self.nodeID = nodeId;
//        self.work = work;
//    }
//
//    return self;
//}

- (instancetype)initNodeId:(NSInteger)nodeId WithWorkHandle:(void(^)(id <Node>))block {
    if (self = [super init]) {
        
        self.nodeID = nodeId;
        self.workBlock = block;
    }
    
    return self;
}

- (void)doWork:(Block)block {
    _onWorkCompleted = block;
//    [self.work doWork:self];
    if (_workBlock) {
        _workBlock(self);
    }
}

- (NSInteger)getId {
    
    return _nodeID;
}

- (void)onCompleted:(NSInteger)note_id {
    
    if (_onWorkCompleted) {
        _onWorkCompleted(note_id);
    }
}

@end

@interface MKWorkFlowManager ()
@property (nonatomic, strong) NSMutableArray *nodes; //!<
@property (nonatomic, assign) BOOL isExecuteAction; //!<
@property (nonatomic, assign) BOOL canExecute; //!<
@end

@implementation MKWorkFlowManager
- (instancetype)init {
    if (self = [super init]) {
        _canExecute = YES;
        NSLog(@"initmethond");
    }
    return self;
}

//看情况可以设计成单例,但是也可以把初始化写到基类里面

- (instancetype)initWithWorks:(NSMutableArray *)works {
    if (self = [super init]) {
        self.nodes = works;
        NSLog(@"initmethond1");
        _canExecute = YES;
    }
    return self;
}

- (void)addNode:(WorkNode *)node {
    [self.nodes addObject:node];
//    if (_canExecute == YES) {
//        if (self.nodes.count == 1) {
//            [self findAndExecuteNextNode];
//        }else{
//            if (!_isExecuteAction && self.nodes.count> 0) {
//                [self executeNextNodeWithNodeId:node.nodeID];
//            }else{
//                 [self findAndExecuteNextNode];
//            }
//        }
//    }

   
}

- (void)hasStartNode {
//    NSLog(@">>>node----%zd",self.nodes.count);
    _isExecuteAction = YES;
}

- (void)pauseExecuteAction {
    _canExecute = NO;
}

- (void)startExecuteAction:(NSInteger)node_id {
    
    if (!_canExecute) {
        _canExecute = YES;
    }
    
    if (node_id == 0) {
       [self findAndExecuteNextNode];
    }else{
       [self executeNextNodeWithNodeId:node_id];
    }
    
}

- (void)startWorking {
    [self findAndExecuteNextNode];
}


- (void)findAndExecuteNextNode {

    if ([self hasWorkingNode]) {
        NSLog(@"还有任务在执行");
        return;
    }
    
    if (self.nodes.count >0 && self.canExecute == YES) {
        WorkNode *worknode = self.nodes.firstObject;
        
        worknode.isWorking = YES;
        
        [worknode doWork:^(NSInteger note_id){
            [self.nodes removeObject:worknode];
        
            if (note_id == 0) {
                [self findAndExecuteNextNode];
            }else{
                [self executeNextNodeWithNodeId:note_id];
            }
        }];
        
//        [self hasStartNode];
    }
    
}




//这个方法优先级高点,即时被中断也执行
- (void)executeNextNodeWithNodeId:(NSInteger)node_id {


    if ([self hasWorkingNode]) {
        NSLog(@"还有任务在执行");
        return;
    }
    
    if (self.nodes.count >0) {
        
        BOOL hasFind = NO;
        
        for (WorkNode *node in self.nodes) {
            
            
            
            if (node.nodeID == node_id) {
                WorkNode *worknode = node;
                node.isWorking = YES;
                
                kWeakSelf;
                [worknode doWork:^(NSInteger note_id){
                    kStrongSelf;
                    [strongSelf.nodes removeObject:worknode];
                
                    if (strongSelf.canExecute) {
                        if (note_id == 0) {
                            [strongSelf findAndExecuteNextNode];
                        }else{
                            [strongSelf executeNextNodeWithNodeId:note_id];
                        }
                    }

                }];
                
                hasFind = YES;
                
//                [self hasStartNode];
                
                break;
            }
        }
        
        if (hasFind != YES) {
            [self findAndExecuteNextNode];
        }

    }
}


- (BOOL)hasWorkingNode{
    BOOL hasWoringNode = NO;
    for (WorkNode *node in self.nodes) {
        if (node.isWorking) {
            hasWoringNode = YES;
            break;
        }
    }
    return hasWoringNode;
}


- (NSMutableArray *)nodes {
    if(!_nodes){
        _nodes = [NSMutableArray array];
    }
    return _nodes;
}

@end



相关文章

  • iOS事件有序处理,比如首页弹窗

    //效果 //今天看到一篇文章试着写了下。https://blog.csdn.net/u014626094/art...

  • 事件传递响应链

    什么是事件传递? 事件传递说白了就是iOS应用程序对用户操作进行逐级、有序处理的过程,这个过程会由UIWindow...

  • RAC常用方法汇总

    一、iOS内部对不同事件的处理 iOS中对不同事件作出响应时,会用不同的方式来处理这些业务逻辑。比如按钮的点击使用...

  • RAC之常用方法汇总

    一、iOS内部对不同事件的处理 iOS中对不同事件作出响应时,会用不同的方式来处理这些业务逻辑。比如按钮的点击使用...

  • iOS 事件以及手势的处理

    iOS 事件以及手势的处理 首先引用深入浅出iOS事件机制,iOS触摸事件处理详解,详解iOS触摸事件与手势识别三...

  • flutter showGeneralDialog 和 show

    showDialog使用 在首页视图使用showDialog弹窗时发现,在iOS上,顶部和下部当有SafeArea...

  • 多线程

    主线程(UI线程) 主线程的主要作用显示和刷新UI界面处理UI事件(比如点击事件、滚动事件、拖拽事件等) iOS中...

  • ios弹窗视图,使用block处理点击事件

    很简单的弹窗视图控件,可以设置箭头方向在视图的上下左右,可以自定义视图背景颜色,判断视图超出屏幕自动改变位置 查看...

  • UI部分-事件处理

    iOS事件处理- 用户使用App产生的事件及响应方法: iOS中不是任何对象都能处理事件,只有继承UIRespon...

  • 由 RunLoop 想到的

    Runloop 是 iOS 提供的一个事件处理机制,当无事件发生时,当前线程会休眠,只有当事件(比如触摸)到来时才...

网友评论

      本文标题:iOS事件有序处理,比如首页弹窗

      本文链接:https://www.haomeiwen.com/subject/lecenctx.html