2013-08-14 7 views
5

द्वारा UICollectionView सेल का चयन मैं UICollectionView उपयोग कर रहा हूँ images.I लोड करने के लिए एक छवि gallery.I UICollectionView सेल के अंदर UIImage इस्तेमाल किया उत्पन्न करने के लिए लंबे समय से प्रेस (नहीं एक टैप द्वारा) द्वारा UICollectionView सेल का चयन करने की जरूरत है।आईओएस: लंबे प्रेस

- (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

आप 'UILongPressGestureRecognizer 'का उपयोग कर सकते हैं – Exploring

उत्तर

0

आप का उपयोग 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

सबसे पहले आपके विचार नियंत्रक से UIGestureRecognizerDelegate जोड़ सकते हैं। फिर अपने ViewController के viewDidLoad() विधि

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

में विधि देर तक दबाए रखने को संभालने के लिए अपने collectionView करने के लिए एक UILongPressGestureRecognizer जोड़ें:

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

इस कोड this answer की ऑब्जेक्टिव-सी संस्करण पर आधारित है।

संबंधित मुद्दे