ios中TableView的长按触发事件
2016-03-02 11:54
441 查看
长按TableView的Cell做一些操作时,就要先捕获TableView的长按事件,然后判断是第几行,或者直接对Cell监控长按事件,对于性能来说,当然是前者好一点,也就是说捕获TableView的长按事件,如下:
.在viewDidLoad事件中加入:
OC代码
监控函数handleLongPress的内容如下:
OC代码
.在viewDidLoad事件中加入:
OC代码
1 | //tableview的长按事件 |
2 | UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] |
3 | initWithTarget:self action:@selector(handleLongPress:)]; //声明长按对象 |
4 | lpgr.minimumPressDuration = 1.0; //seconds 设置长按多长事件触发长按事件 |
5 | [self.tableView addGestureRecognizer:lpgr]; //把该事件加载到tableview对象上 |
OC代码
01 | -( void )handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer |
02 | { |
03 | CGPoint p = [gestureRecognizer locationInView:self.tableView]; //点击的位置 |
04 | NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p]; //根据点击的坐标得出点击的indexpath |
05 | if (gestureRecognizer.state == UIGestureRecognizerStateBegan) //长按事件开始 |
06 | { |
07 | UIAlertView * addView = [[UIAlertView alloc] initWithTitle: @"提示" message: @"加入书签,是否继续?" delegate :self cancelButtonTitle: @"确定" otherButtonTitles: @"取消" ,nil]; |
08 | addView.alertViewStyle = UIAlertViewStyleDefault; |
09 | [addView show]; |
10 | self.tableviewcurrentcoursename=[[self.fetchedResultsController objectAtIndexPath:indexPath] name]; |
11 | } |
12 | else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) //长按事件结束 |
13 | { |
14 | NSLog( @"UIGestureRecognizerStateEnded" ); |
15 | } |
16 | else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) |
17 | { |
18 | NSLog( @"UIGestureRecognizerStateChanged" ); |
19 | } |
20 | else if (gestureRecognizer.state == UIGestureRecognizerStateCancelled) |
21 | { |
22 | NSLog( @"UIGestureRecognizerStateCancelled" ); |
23 | } |
24 | else if (gestureRecognizer.state ==UIGestureRecognizerStateFailed) |
25 | { |
26 | NSLog( @"UIGestureRecognizerStateFailed" ); |
27 | } |
28 | } |
相关文章推荐
- iOS 谁说程序猿不懂浪漫之 爱心
- 升级iOSBeta版本
- iOS开发之内存管理:借助工具解决内存问题
- IOS--多线程基础
- iOS 判断字符串"<null>"类型
- iOS开发之JS数据交互
- ios 开发证书显示“此证书的签发者无效”解决方法
- iOS中根据生日计算星座♈️
- Xcode Archive 不生成dsym文件的解决方法
- iOS实现从背景图中取色的代码
- iOS Error Domain=com.alamofire.error.serialization.response Code=-1016
- iOS 删除 Main.storyboard 和 LaunchScreen.storyboard
- iOS 日期字符串相互转换
- iOS中KeyChain的使用
- 打包ipa因“此证书的签发者无效” 导致的 “Missing iOS Distribution signing identity for”...的问题
- iOS多线程技术
- ios 文本转语音 播报 AVSpeechSynthesizer
- Nagios中常见参数的含义
- iOS获取设备型号、设备类型等信息
- iOS多线程之NSOperation的使用