2013-08-14 15 views
5

tarafından UICollectionView Cell seçilmesi Uzun Press (değil tek bir dokunma ile) tarafından UICollectionView Cell seçmeniz gerekir.IOS: Ben images.I yüklemek için gallery.I <code>UICollectionView</code> Cell iç <code>UIImage</code> kullanılan bir görüntü oluşturmak için <code>UICollectionView</code> kullanıyorum Uzun Press

- (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' özelliğini kullanabilirsiniz. – Exploring

cevap

0

Sen

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

LongPressGesture Önce görünümü denetleyicisine UIGestureRecognizerDelegate eklemek kullanabilirsiniz. Sonra uzun basın işlemek için viewcontroller en viewDidLoad() yöntemiyle

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

Bu kod this answer ait Objective-C sürümü dayanmaktadır.

İlgili konular