用block将视图中的点击事件写出去

第一步,加入这个宏

///  安全运行block
#define BLOCK_SAFE_RUN(block, ...) block ? block(__VA_ARGS__) : nil;

#ifndef weakify

#if DEBUG

#if __has_feature(objc_arc)

#define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;

#else

#define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;

#endif

#else

#if __has_feature(objc_arc)

#define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;

#else

#define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;

#endif

#endif

#endif



#ifndef strongify

#if DEBUG

#if __has_feature(objc_arc)

#define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;

#else

#define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;

#endif

#else

#if __has_feature(objc_arc)

#define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;

#else

#define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;

#endif

#endif

#endif

第二步,设置按钮的点击事件属性block

@interface ContentEditFooterView : UIView

@property (nonatomic,   copy) void (^pictureBlock)(void);
@property (nonatomic,   copy) void (^smileBlock)(void);
@property (nonatomic,   copy) void (^linkBlock)(void);
@property (nonatomic,   copy) void (^cancelBlock)(void);
@property (nonatomic,   copy) void (^tagBlock)(void);

@property (nonatomic, strong) ERichTextToolBar *toolBar;

- (instancetype)initWithFrame:(CGRect)frame editView:(ERichTextEditorView *)editView;

第三步,在按钮的点击事件中和block属性关联


- (void)click:(UIButton *)button {
   switch (button.tag - 200) {
       case 0:
           BLOCK_SAFE_RUN(self.pictureBlock);
           break;
       case 1:
           BLOCK_SAFE_RUN(self.smileBlock);
           break;
       case 2:
       {
           if (self.height == 44) {
               self.height += 44;
               self.y -= 44;
               _toolBar.hidden = NO;
           } else {
               self.height -= 44;
               self.y += 44;
               _toolBar.hidden = YES;
           }
           _footerView.y = self.height - kFooterH;
       }
           break;
       case 3:
           BLOCK_SAFE_RUN(self.linkBlock);
           break;
       case 4:
           [_editorView undo];
           BLOCK_SAFE_RUN(self.cancelBlock);
           break;
       case 5:
           BLOCK_SAFE_RUN(self.tagBlock);
           break;
       default:
           break;
   }
}

第四步,在如下方法中实现具体的点击事件

- (ContentEditFooterView *)footerView {
    if (!_footerView) {
        _footerView = [[ContentEditFooterView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - TAB_BAR_SAFE_BOTTOM_MARGIN - kFooterHeight, SCREEN_WIDTH, kFooterHeight) editView:self.editorView];
        @weakify(self);
        _footerView.pictureBlock = ^{
            @strongify(self);
            [self.editorView recordCursorPosition];
            
            // 以下为测试代码,若从本地选择图片,请先传给后端拿到url,再依照以下步骤进行调用
            NSArray *imgUrls = @[
                                 @"http://img.i.cacf.cn/thread/1803/21/252d6546e6224c8b694a8d1918e62410.jpg",
                                 @"http://img.i.cacf.cn/thread/1803/21/1f061a159effab856007bb5e8466ad96.jpg",
                                 @"http://img1.imgtn.bdimg.com/it/u=397740769,1564679694&fm=200&gp=0.jpg",
                                 @"http://img0.imgtn.bdimg.com/it/u=4119238346,1821169886&fm=27&gp=0.jpg",
                                 @"http://img5.imgtn.bdimg.com/it/u=1858283759,2806224052&fm=200&gp=0.jpg",
                                 @"http://img0.imgtn.bdimg.com/it/u=2711668609,3709801299&fm=200&gp=0.jpg",
                                 @"http://img5.imgtn.bdimg.com/it/u=2239397734,1608218617&fm=27&gp=0.jpg"
                                 ];
            for (NSInteger i = imgUrls.count - 1; i >= 0; i --) {
                [self.editorView insertImage:imgUrls[i]];
            }
        };
        _footerView.tagBlock = ^{
            
        };
        
    }
    return _footerView;
}

你可能感兴趣的:(用block将视图中的点击事件写出去)