您的位置:首页 > 产品设计 > UI/UE

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) {
      //长按事件结束
   }
}   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  长按手势