通過(guò)tag獲取控件,實(shí)現(xiàn)image數(shù)組添加點(diǎn)擊事件

最近小白項(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)擊了");};

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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