最近小白項(xiàng)目里遇到一些問(wèn)題,讓小白很抓狂吖!其中一個(gè)就是如何通過(guò)tag獲取控件,實(shí)現(xiàn)image數(shù)組添加點(diǎn)擊事件的問(wèn)題,其實(shí)不難的,可是小白只知道UIBtton數(shù)組用tag值來(lái)區(qū)分控件實(shí)現(xiàn)點(diǎn)擊,還有就是單張imageView用UITapGestureRecognizer來(lái)實(shí)現(xiàn)點(diǎn)擊方法,
//添加單擊手勢(shì)
UITapGestureRecognizer* singleRecognizer;
singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
singleRecognizer.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:singleRecognizer];
imageView.userInteractionEnabled=YES;
但現(xiàn)在的問(wèn)題是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被點(diǎn)擊了");};
cell.image1Block = ^{NSLog(@"圖1被點(diǎn)擊了");};
cell.image2Block = ^{NSLog(@"圖2被點(diǎn)擊了");};
cell.image3Block = ^{NSLog(@"圖3被點(diǎn)擊了");};
cell.image4Block = ^{NSLog(@"圖4被點(diǎn)擊了");};