简单模拟写个UIview视图交互,传递闭包。
.h文件
#import <UIKit/UIKit.h>
@interface ZDX : UIView
@property (nonatomic, copy) void (^addPhotoBlock)(void);
@end
.m文件
#import "ZDX.h"
@implementation ZDX
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor redColor];
[self setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addPhoto)];
[self addGestureRecognizer:tapGes];
}
return self;
}
- (void)addPhoto {
/// 如果闭包属性不为空,不做处理,为空则传递闭包属性。
!self.addPhotoBlock ? : self.addPhotoBlock();
}
@end
调用:
ZDX *zz = [[ZDX alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[self.view addSubview:zz];
zz.addPhotoBlock = ^{
NSLog(@"sss");
};
网友评论