2015-03-24 13 views
18

Bir hücreye uzun basıldığında UICollectionViewCell dizin dizininin nasıl yazdırılacağını öğrenmek istiyorum.UILongPressGestureRecognizer'ı Swift'deki bir UICollectionViewCell ile nasıl kullanırım?

Swift'de bunu nasıl yapabilirim?

Bunun nasıl yapılacağına dair bir örnek için her yere baktım; Swift'de bir tane bulamıyorum.

+0

Bize neyi denediğinizi gösterin. – rdelmar

+0

Ç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

cevap

56

İ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

+0

Teşekkürler. Mükemmel! – webmagnets

+0

çalıştı. yukarı oy. – kennydust

+0

Benim dikey kaydırma benim koleksiyonum için çalışmıyorSonra: (\ –

3

bir şey olmasıydı pin zamanlayıcı gecikme sağlandığı anda pin görünür izin verirken yerleşimleri pin.

Ayrıca yukarıda bir yazım hatası: Reconizer -> Tanıyıcı

+0

Kesinlikle haklısınız, daha fazla bastırmak için basın serbest bırakılıncaya kadar beklemek yerine hala yerleştirirken pin yerleştirilen yerli harita gibi daha fazla hissediyor Burada tam Swift kodu var: http://stackoverflow.com/a/29466391/1359088 –

8

ztan cevap en hızlı 3 sözdizimi dönüştürülür:

func handleLongPress(_ gestureReconizer: UILongPressGestureRecognizer) { 
    if gestureReconizer.state != UIGestureRecognizerState.ended { 
     return 
    } 

    let p = gestureReconizer.location(in: collectionView) 
    let indexPath = collectionView.indexPathForItem(at: p) 

    if let index = indexPath { 
     var cell = collectionView.cellForItem(at: index) 
     // do stuff with your cell, for example print the indexPath 
     print(index.row) 
    } else { 
     print("Could not find index path") 
    } 
} 
3

handleLongProgress hızlı 3 sözdizimi dönüştürülür yöntem gayet iyi çalışıyor. Sadece lpgr'nin başlatılmasının şu şekilde değiştirileceğini eklemek istiyorum:

let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gestureReconizer:)))