最近小白项目里遇到一些问题,让小白很抓狂吖!其中一个就是如何通过tag获取控件,实现image数组添加点击事件的问题,其实不难的,可是小白只知道UIBtton数组用tag值来区分控件实现点击,还有就是单张imageView用UITapGestureRecognizer来实现点击方法,
//添加单击手势
UITapGestureRecognizer* singleRecognizer;
singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
singleRecognizer.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:singleRecognizer];
imageView.userInteractionEnabled=YES;
但现在的问题是imageArray,小白解决方法是:
UICollectionViewCell的.h文件
@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *imageArray;
@property(nonatomic,copy)void(^image0Block)();
@property(nonatomic,copy)void(^image1Block)();
@property(nonatomic,copy)void(^image2Block)();
@property(nonatomic,copy)void(^image3Block)();
@property(nonatomic,copy)void(^image4Block)();
@end
UICollectionViewCell的.m文件
UIButton *btn0 = [self.imageArray[0] viewWithTag:0];
[btn0 bk_whenTapped:^{
if (self.image0Block) {
self.image0Block();
}
}];
UIButton *btn1 = [self.imageArray[1] viewWithTag:1];
[btn1 bk_whenTapped:^{
if (self.image1Block) {
self.image1Block();
}
}];
UIButton *btn2 = [self.imageArray[2] viewWithTag:2];
[btn2 bk_whenTapped:^{
if (self.image2Block) {
self.image2Block();
}
}];
UIButton *btn3 = [self.imageArray[3] viewWithTag:3];
[btn3 bk_whenTapped:^{
if (self.image3Block) {
self.image3Block();
}
}];
UIButton *btn4 = [self.imageArray[4] viewWithTag:4];
[btn4 bk_whenTapped:^{
if (self.image4Block) {
self.image4Block();
}
}];
ViewController.m
cell.image0Block = ^{NSLog(@"图0被点击了");};
cell.image1Block = ^{NSLog(@"图1被点击了");};
cell.image2Block = ^{NSLog(@"图2被点击了");};
cell.image3Block = ^{NSLog(@"图3被点击了");};
cell.image4Block = ^{NSLog(@"图4被点击了");};
网友评论