2014-04-07 32 views
8

Seçilen UICollectionviewCell içinde dokunma konumunu almaya çalışıyorum. Görünüm olarak UICollectionView, collectionView içeren bir UIViewController var. CollectionView içinde UICollectionViewCells vardır. Vc, UICollectionViewDelegate ile uyumludur, bu nedenle, tercihen delegenin geri çağrısına, collectionView:(UICollectionView *)collection didSelectItemAtIndexPath:(NSIndexPath *)indexPath, (mümkünse) dokunulan yeri almak istiyorum. Bunu nasıl yapacağınız konusunda herhangi bir öneriniz var mı? Teşekkürler.uicollectionviewcell'in dokunmatik konumunu alma

+--------------------------------------------+ 
| UICollectionView       | 
|           | 
| +-------------+  +-------------+  | 
| | UICollec |  |    |  | 
| | tionView |  |    |  | 
| | Cell  |  |  *<---------------<Touch here 
| |    |  |    |  | 
| +-------------+  +-------------+  | 
|           | 
+--------------------------------------------+  

* UPDATE * I UIGestureRecognizer kullanılarak UIViewCollectionView hafifçe vurun tespit edilmesi ve daha sonra UICollectionViewCell görünümüne musluk noktası dönüştürme sona erdi. Daha iyi bir yol varsa lütfen bana bildirin. Teşekkürler.

-(void) viewDidLoad { 
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; 
    [tapGesture setNumberOfTapsRequired:1]; 
    [self.collectionView addGestureRecognizer:tapGesture]; 
} 

-(void) handleTap:(UIGestureRecognizer*) gesture { 
    if (gesture.state == UIGestureRecognizerStateEnded) { 
    CGPoint tappedPoint = [gesture locationInView:_collectionView]; 
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:tappedPoint]; 
    CollectionViewCell *cell = (CollectionViewCell*)[self.collectionView cellForItemAtIndexPath:indexPath]; 
    CGPoint pointWRTCell = [cell convertPoint:tappedPoint fromView:self.collectionView]; 
    NSLog(@"collectionView point(%1.1f,%1.1f); cell point (%1.1f,%1.1f)", 
      tappedPoint.x,tappedPoint.y,pointWRTCell.x,pointWRTCell.y); 
    } 
} 

cevap

5

Bunu yapmak için muhtemelen özel bir hücreye ihtiyacınız olacaktır. didSelectItemAtIndexPath sadece size bir hücre seçildiğini söyler. Hücrenin içinde nereye dokunulduğu değil.

+0

Yanıtınız için teşekkür ederiz. Alt sınıf 'UICollectionView' çalışabilir. Ben 'UITabGestureRecognizer' kullanarak sona erdi ve 'UICollectionView' dan 'UICollectionViewCell' – Loozie

+0

'e dokunulan konumu dönüştürdüm Bazı içerikler daha iyi bir çözüm bulmamıza yardımcı olabilir. Çözmeye çalıştığın problem nedir? – CrimsonChris

+0

Bir hücrenin belirli bir yerini (hücrenin kendisi değil) – Loozie

1

UIView, func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? yöntemine sahiptir. Temas noktasını almak için, bu görünümü koleksiyon görünümünüzde veya koleksiyon görünümü hücresinde geçersiz kılabilirsiniz.

Farklı koordinat sistemlerinde noktayı dönüştürmeniz için func convertPoint(_ point: CGPoint, toView view: UIView?) -> CGPoint gibi yöntemler vardır.

0

.. bu kodu denemek için indexPath alın

(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellId" forIndexPath:[indexPath row]]; 

[[cell myButton] addTarget:self action:@selector(myClickEvent:event:) forControlEvents:UIControlEventTouchUpInside]; 

return cell; 

} 


(IBAction)myClickEvent:(id)sender event:(id)event { 

NSSet *touches = [event allTouches]; 

UITouch *touch = [touches anyObject]; 

CGPoint currentTouchPosition = [touch locationInView:_myCollectionArray]; 

NSIndexPath *indexPath = [_myCollectionArray indexPathForItemAtPoint: currentTouchPosition]; 

} 
0

Sen UICollectionViewCell dokunmatik konuma kaydetmek için özellik eklemek gerekir.

@interface YourCollectionViewCell : UICollectionViewCell 
@property (nonatomic, assign) CGPoint  touchPoint; 
@end 

Sonra

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    YourCollectionViewCell * cell = [collectionView cellForItemAtIndexPath:indexPath]; 
    NSLog(@"touch location %f, %f", cell.touchPoint.x, cell.touchPoint.y); 
} 

Ben almak için daha kolay bir yolu olduğunu düşünüyorum Eğer UICollectionView içinde temas noktası kullanabilir,

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesBegan:touches withEvent:event]; 
    self.touchPoint = [[touches anyObject] locationInView:self]; 
} 

Nihayet YourCollectionViewCell.m içinde touchesBegan arasında etkinlik ekle konuma dokunun. Ve bunu kodlarımda kullanıyorum.

İlgili konular