2013-08-14 7 views
5

私はgallery.Iロングプレス(ないシングルタップすることにより)でUICollectionViewセルを選択する必要がありimages.IをロードするためにUICollectionViewセル内のUIImageを使用した画像を生成するUICollectionViewを使用しています。IOS:ロングプレスによって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

あなたはまず、あなたのビューコントローラにUIGestureRecognizerDelegateを追加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

を使用することができます。

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のObjective-Cのバージョンに基づいています。そして、あなたの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を追加します。

関連する問題