İlk önce denetleyicinizin UIGestureRecognizerDelegate
olması gerekir. Sonra uzun basın işlemek için viewcontroller en viewDidLoad()
yöntemle
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)
}
daki CollectionView'ın yöntemi bir UILongPressGestureRecognizer ekleyin:
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")
}
}
Bu kod this answer ait Objective-C sürümü dayanmaktadır. Eğer sorun yok longpress, serbest bırakana kadar
if gestureReconizer.state != UIGestureRecognizerState.Ended {
return
}
raptiye yerleştirmek değil, ama birden önleyecektir bütün fonksiyon etrafında
if gestureRecognizer.state == UIGestureRecognizerState.Began { }
bulundu: buldum
Bize neyi denediğinizi gösterin. – rdelmar
Çok fazla şeyi denedim, hepsini buraya koymak imkansız olurdu. Bu bağlantıları kullanarak bilgiyi kullanmayı denedim: http://stackoverflow.com/questions/23392485/change-background-of-uicollectionview-cell-on-tap - http://www.tagwith.com/question_73045_delete-uicollectionviewcell- with-uilongpressgesturerecognizer? ref = driverlayer.com/web - http://www.freshconsulting.com/create-drag-and-drop-uitableview-swift/ Diğer sayfaların TON'ları. – webmagnets