您的位置:首页 > 移动开发 > Swift

swift3.0 collectionView添加长按手势识别

2017-10-27 14:14 423 查看
1.  在collectionView绘制cllectionViewCell的代理函数中添加长按识别:

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(cellLongPress(sender:)))
cell.addGestureRecognizer(longPress)


2. 识别长按的位置

func cellLongPress(sender:UILongPressGestureRecognizer) {

        //获得点击位置

        let touchPoint = sender.location(in: self.collectionView)

        //识别是否长按后松开的手势

        if (sender.state == UIGestureRecognizerState.ended)

        {

            let indexPath = self.collectionViewInput.indexPathForItem(at: touchPoint)

           

            if indexPath != nil

            {
print("你点击的是第\(indexPath_output!.row + 1)个cell")    

            }

           

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios swift 手势识别
相关文章推荐