用block將視圖中的點(diǎn)擊事件寫出去

第一步,加入這個(gè)宏

///  安全運(yùn)行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

第二步,設(shè)置按鈕的點(diǎn)擊事件屬性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;

第三步,在按鈕的點(diǎn)擊事件中和block屬性關(guān)聯(lián)


- (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;
   }
}

第四步,在如下方法中實(shí)現(xiàn)具體的點(diǎn)擊事件

- (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];
            
            // 以下為測(cè)試代碼,若從本地選擇圖片,請(qǐng)先傳給后端拿到url,再依照以下步驟進(jìn)行調(diào)用
            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;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時(shí)...
    歐辰_OSR閱讀 30,228評(píng)論 8 265
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,644評(píng)論 1 32
  • 高艷峰 信陽 網(wǎng)絡(luò)初級(jí)九期 堅(jiān)持原創(chuàng)分享第十二天 2018-03-08 我不知道我現(xiàn)在是穩(wěn),還是放任? 開...
    gyf16閱讀 167評(píng)論 0 0
  • 1)We are in the midst of the fourth major oil investment ...
    伍帆閱讀 654評(píng)論 0 0

友情鏈接更多精彩內(nèi)容