IOS开发: 为UIImageView添加点击事件
2020-02-16 20:23
363 查看
转载于:http://www.pocketdigi.com/20140218/1276.html
UIImageView并不像UIButton一样,点点鼠标就可以关联点击事件,也不像Android里有onClickListener,这里需要借助于UITapGestureRecognizer类。从类名上就可以看出,这个类就是用于处理tap(单击)事件的。
bbc和voaspecial是UIImageView对象
[bbc setUserInteractionEnabled:YES]; [voaspecial setUserInteractionEnabled:YES]; [bbc addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCategory:)]]; [voaspecial addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCategory:)]];
经测试,多个UIImageView不能共用一个UITapGestureRecognizer对象,之前调用的会没效果。
根据点击的view判断来自谁的点击。
-(void)clickCategory:(UITapGestureRecognizer *)gestureRecognizer { NSLog(@"click"); NSLog(@"%hhd",[gestureRecognizer isMemberOfClass:[UITapGestureRecognizer class]]); UIView *viewClicked=[gestureRecognizer view]; if (viewClicked==bbc) { NSLog(@"bbc"); }else if(viewClicked==voaspecial) { NSLog(@"voaspecial"); } }
转载于:https://www.cnblogs.com/WenJim/p/4808204.html
- 点赞
- 收藏
- 分享
- 文章举报
![](https://g.csdnimg.cn/static/user-reg-year/1x/5.png)
相关文章推荐
- iOS UIAlertController学习笔记
- UIImagePickerController
- UIImagePickerController---图片添加---初级使用
- UI控件---CollectionView
- UIScrollView约束的添加
- UITableViewCell与cell之间的那条线
- Sky TV ruling begs the question 'Why can't we let British companies succeed?'
- 认识arduino入门必备知识
- iOS-UIButton简单的使用介绍
- UITableView个人笔记
- Arduino超声波传感器测量距离
- hardware-fireduino
- arduino于RFID
- arduino与RF
- XLMHttpRequest对象的status属性,readyState属性以及onreadystatechange事件
- Aion Quest: Go Back into the Frillneck Habitat
- ValueError: Invalid configuration: - Deprecated option 'domaincontroller': use 'http_authenticator
- 深度学习入门二分类例子中ValueError: Object arrays cannot be loaded when allow_pickle=False
- [转]flash builder 4 编译器参数
- STL stack和queue的用法