iOS development-use NSOperation for gift sending in live video media

iOS development-use NSOperation for gift sending in live video media

Preface

  • NSOperation needs to be used in iOS development to send gifts, such as in live video.

effect

Insert picture description here

Preparation before development

  • pod import
    pod 'SDWebImage'
    pod 'YYModel'

Key code

  • LDSGiftOperation.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@class LDSGiftModel,LDSGiftShowView;
typedef void(^completeOpBlock)(BOOL finished,NSString *giftKey);
@interface LDSGiftOperation : NSOperation

/**
 增加一个操作

 @param giftShowView 礼物显示的View
 @param backView 礼物要显示在的父view
 @param model 礼物的数据
 @param completeBlock 回调操作结束
 @return 操作
 */
+ (instancetype)addOperationWithView:(LDSGiftShowView *)giftShowView
                              OnView:(UIView *)backView
                                Info:(LDSGiftModel *)model
                       completeBlock:(completeOpBlock)completeBlock;


/** 礼物展示的父view */
@property(nonatomic,strong) UIView *backView;
/** model */
@property(nonatomic,strong) LDSGiftModel *model;

/** block */
@property(nonatomic,copy) completeOpBlock opFinishedBlock;
/** showview */
@property(nonatomic,strong) LDSGiftShowView *giftShowView;

@end

  • LDSGiftOperation.m
#import "LDSGiftOperation.h"
#import "LDSGiftShowView.h"
#import "LDSGiftModel.h"

@implementation LDSGiftOperation

@synthesize finished = _finished;
@synthesize executing = _executing;

+ (instancetype)addOperationWithView:(LDSGiftShowView *)giftShowView
                              OnView:(UIView *)backView
                                Info:(LDSGiftModel *)model
                       completeBlock:(completeOpBlock)completeBlock {
    
    
    LDSGiftOperation *op = [[LDSGiftOperation alloc] init];
    op.giftShowView = giftShowView;
    op.model = model;
    op.backView = backView;
    op.opFinishedBlock = completeBlock;
    return op;
}

- (instancetype)init {
    
    
    if (self = [super init]) {
    
    
        _executing = NO;
        _finished  = NO;
    }
    return self;
}

- (void)start {
    
     //子类要实现的方法
    if ([self isCancelled]) {
    
    
        _finished = YES;
        return;
    }
    
    _executing = YES;
    //NSLog(@"当前队列-- %@",self.model.giftName);
    __weak typeof(self) weakSelf = self;
    //NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
    
    
        NSLog(@"currentThread - %@", [NSThread currentThread]);
        [weakSelf.backView addSubview:weakSelf.giftShowView];
        
        [weakSelf.giftShowView showGiftShowViewWithModel:weakSelf.model completeBlock:^(BOOL finished,NSString *giftKey) {
    
    
            weakSelf.finished = finished;
            if (weakSelf.opFinishedBlock) {
    
    
                weakSelf.opFinishedBlock(finished,giftKey);
            }
        }];
    }];
    
}

#pragma mark - 手动触发 KVO

- (void)setExecuting:(BOOL)executing {
    
    
    [self willChangeValueForKey:@"isExecuting"];
    _executing = executing;
    [self didChangeValueForKey:@"isExecuting"];
}

- (void)setFinished:(BOOL)finished {
    
    
    [self willChangeValueForKey:@"isFinished"];
    _finished = finished;
    [self didChangeValueForKey:@"isFinished"];
}

@end

Demo

Guess you like

Origin blog.csdn.net/weixin_41732253/article/details/110279259