UILongPressGestureRecognizer长按手势
2016-06-23 17:46
477 查看
- (void)viewDidLoad
{
//长按
//创建长按的手势
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
//设置长按的时间
press.minimumPressDuration = 0.5;//2秒(默认0.5秒)
//设置长按的手指数
[press setNumberOfTouchesRequired:1];
//把手势加到界面上,可以加到任何控件上,需要开启用户交互
// _lable.userInteractionEnabled = YES;
// [self.view addGestureRecognizer:press];
[cell addGestureRecognizer:press];
}
//长按后触发该方法
给tableview或者collectionView的cell添加长按手势执行两次(UILongPressGestureRecognizer)开始一次结束一次
-(void)longPressAction:(UILongPressGestureRecognizer*)longPress
{
if ([longPress state] ==UIGestureRecognizerStateBegan) {
//长按事件开始"
// 可以获取我们在哪个cell上长按 tableview collectionView
CGPoint point = [longPress locationInView:_collectionView];
//CGPoint point = [longPress locationInView:_tableView];
NSIndexPath *indexPath = [_collectionView indexPathForItemAtPoint:point];
//NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:point];
if (indexPath != nil) {
NSLog(@"%ld", indexPath.row);
}
}
else if ([longPress state] ==UIGestureRecognizerStateEnded) {
//长按事件结束
}
}
{
//长按
//创建长按的手势
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
//设置长按的时间
press.minimumPressDuration = 0.5;//2秒(默认0.5秒)
//设置长按的手指数
[press setNumberOfTouchesRequired:1];
//把手势加到界面上,可以加到任何控件上,需要开启用户交互
// _lable.userInteractionEnabled = YES;
// [self.view addGestureRecognizer:press];
[cell addGestureRecognizer:press];
}
//长按后触发该方法
给tableview或者collectionView的cell添加长按手势执行两次(UILongPressGestureRecognizer)开始一次结束一次
-(void)longPressAction:(UILongPressGestureRecognizer*)longPress
{
if ([longPress state] ==UIGestureRecognizerStateBegan) {
//长按事件开始"
// 可以获取我们在哪个cell上长按 tableview collectionView
CGPoint point = [longPress locationInView:_collectionView];
//CGPoint point = [longPress locationInView:_tableView];
NSIndexPath *indexPath = [_collectionView indexPathForItemAtPoint:point];
//NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:point];
if (indexPath != nil) {
NSLog(@"%ld", indexPath.row);
}
}
else if ([longPress state] ==UIGestureRecognizerStateEnded) {
//长按事件结束
}
}
相关文章推荐
- 26.手势识别之UILongPressGestureRecognizer
- ios拼图游戏(四)之触摸交换拼图
- ios 复制字符串到剪贴板
- 手势 UILongPressGestureRecognizer 长按手势
- WKWebView之长按手势
- UIGestureRecognizer 6种手势用法介绍
- ios 拖动重新排序
- Count Numbers with Unique Digits
- niagara编译错误:using type arguments to access generic types requires the use of "-source 1.5"
- 给UICollectionViewCell绑定手势 long press举例
- button,input,html 对应的value,innerText,innerHTML 关系
- NGUI OnChange Event
- easyui_动态添加隐藏toolbar按钮
- easyui numberbox不可编辑
- 点击UITableView的cell展开收缩
- iOS UITableViewCell左滑删除
- Spark RDD使用详解4--Key-Value型Transformation算子
- Spark RDD使用详解3--Value型Transformation算子
- 错误Building an MFC project for a non-Unicode character set is deprecated
- AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案