内容简介:当我长时间按下一个单元格时,我想知道如何打印一个UICollectionViewCell的indexPath.如何在Swift中做到这一点?我已经看了一遍如何做到这一点的例子;在斯威夫特找不到一个.
当我长时间按下一个单元格时,我想知道如何打印一个UICollectionViewCell的indexPath.
如何在Swift中做到这一点?
我已经看了一遍如何做到这一点的例子;在斯威夫特找不到一个.
首先你的视图控制器需要是UIGestureRecognizerDelegate.然后在您的viewcontroller的viewDidLoad()方法中将一个UILongPressGestureRecognizer添加到您的collectionView
class ViewController: UIViewController, UIGestureRecognizerDelegate { override func viewDidLoad() { super.viewDidLoad() let lpgr = UILongPressGestureRecognizer(target: self, action: "handleLongPress:") lpgr.minimumPressDuration = 0.5 lpgr.delaysTouchesBegan = true lpgr.delegate = self self.collectionView.addGestureRecognizer(lpgr) }
处理长按的方法:
func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) { if gestureReconizer.state != UIGestureRecognizerState.Ended { return } let p = gestureReconizer.locationInView(self.collectionView) let indexPath = self.collectionView.indexPathForItemAtPoint(p) if let index = indexPath { var cell = self.collectionView.cellForItemAtIndexPath(index) // do stuff with your cell, for example print the indexPath println(index.row) } else { println("Could not find index path") } }
该代码基于Objective-C版本的 this answer .
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/29241691/how-do-i-use-uilongpressgesturerecognizer-with-a-uicollectionviewcell-in-swift
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- RecyclerView使用指南(一)—— 基本使用
- 如何使用Meteorjs使用URL参数
- 使用 defer 还是不使用 defer?
- 使用 Typescript 加强 Vuex 使用体验
- [译] 何时使用 Rust?何时使用 Go?
- UDP协议的正确使用场合(谨慎使用)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
尽在双11:阿里巴巴技术演进与超越
阿里巴巴集团双11技术团队 / 电子工业出版社 / 2017-4 / 79
“双 11”,诞生于杭州,成长于阿里,风行于互联网,成就于新经济,贡献于全世界。 从 2009 年淘宝商城起,双 11 已历经八年。每年的双 11 既是当年的结束,又是走向未来的起点。技术的突破创新,商业模式的更替交互,推动着双 11 迈步向前。 《尽在双11——阿里巴巴技术演进与超越》是迄今唯一由阿里巴巴集团官方出品、全面阐述双 11 八年以来在技术和商业上演进和创新历程的书籍。内容......一起来看看 《尽在双11:阿里巴巴技术演进与超越》 这本书的介绍吧!