2013-08-14 13 views
5

Sto utilizzando UICollectionView per generare una galleria di immagini. Ho usato UIImage all'interno della cella UICollectionView per caricare le immagini. Devo selezionare UICollectionView Cell da Long Press (non da un singolo tocco).IOS: selezione della cella UICollectionView a lunga pressione

- (IBAction)longPress:(UILongPressGestureRecognizer *)gestureRecognizer 
{ 

    UICollectionViewCell *cell=(UICollectionViewCell *)[gestureRecognizer view]; 
    int index=cell.tag; 

    OverlayImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width,  cell.frame.size.height)]; 
    OverlayImage.image = [UIImage imageNamed:@"[email protected]"]; 
    [cell addSubview:OverlayImage]; 

} 
+0

È possibile utilizzare 'UILongPressGestureRecognizer' – Exploring

risposta

0

È possibile utilizzare LongPressGesture

UILongPressGestureRecognizer *longpressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)]; 
    longpressGesture.minimumPressDuration = 5; 
    [longpressGesture setDelegate:self]; 
    [self.yourImage addGestureRecognizer:longpressGesture]; 


    - (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer { 
     NSLog(@"longPressHandler"); 
     UIImageView *tempImage=(UIImageView*)[gestureRecognizer view]; 
    } 
10

aggiungere prima UIGestureRecognizerDelegate al controller della vista. Quindi aggiungere un UILongPressGestureRecognizer al vostro CollectionView nel metodo

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) 
} 

del viewcontroller viewDidLoad() Il metodo per gestire pressione lunga:

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

    let point = gestureReconizer.locationInView(self.collectionView) 
    let indexPath = self.collectionView.indexPathForItemAtPoint(point) 

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

Questo codice è basato sulla versione Objective-C di this answer.

Problemi correlati