2014-04-07 27 views
8

Tôi đang cố gắng để có được vị trí cảm ứng bên trong UICollectionviewCell đã chọn. Tôi có một UIViewController, vc, chứa một UICollectionView, collectionView, như là khung nhìn của nó. Bên trong collectionView là UICollectionViewCells. Các vc phù hợp với các UICollectionViewDelegate tốt nhất là tôi muốn có được vị trí xúc động bên trong gọi lại đại biểu, collectionView:(UICollectionView *)collection didSelectItemAtIndexPath:(NSIndexPath *)indexPath, (nếu có thể). Bất kỳ đề xuất về cách làm điều đó? Cảm ơn.liên lạc với vị trí của uicollectionviewcell

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

* CẬP NHẬT * tôi đã kết thúc phát hiện vòi nước trên UIViewCollectionView sử dụng UIGestureRecognizer và sau đó chuyển đổi điểm tap vào xem UICollectionViewCell. Vui lòng cho tôi biết nếu có cách nào tốt hơn. Cảm ơn.

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

Trả lời

5

Có thể bạn sẽ cần một ô tùy chỉnh để thực hiện việc này. didSelectItemAtIndexPath chỉ cho bạn biết một ô đã được chọn. Không phải bên trong ô mà nó chạm vào.

+0

Cảm ơn bạn đã trả lời. "UICollectionView" của lớp con có thể hoạt động. Tôi đã kết thúc bằng cách sử dụng 'UITabGestureRecognizer' và chuyển đổi vị trí khai thác từ 'UICollectionView' thành 'UICollectionViewCell' – Loozie

+0

Một số ngữ cảnh có thể giúp chúng tôi đưa ra giải pháp tốt hơn. Vấn đề bạn đang cố gắng giải quyết là gì? – CrimsonChris

+0

Tôi chỉ đơn giản muốn xóa một ô khi một người dùng tab một vị trí cụ thể của một tế bào (không phải là chính tế bào) – Loozie

1

UIView có phương thức func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView?. Để có được điểm tiếp xúc, bạn có thể ghi đè phương thức này trong chế độ xem bộ sưu tập hoặc ô xem bộ sưu tập.

Và có các phương pháp như func convertPoint(_ point: CGPoint, toView view: UIView?) -> CGPoint để bạn chuyển đổi điểm trong các hệ tọa độ khác nhau.

0

Nhận indexPath thử mã này ..

(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

Bạn nên thêm tài sản để lưu giữ vị trí touch trong UICollectionViewCell.

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

Sau đó, thêm trường hợp touchesBegan trong YourCollectionViewCell.m

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

Cuối cùng, bạn có thể sử dụng hệ trong UICollectionView bạn

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

Tôi nghĩ rằng đó là cách dễ dàng hơn để có được những chạm vào vị trí. Và tôi đang sử dụng nó trong mã của tôi.

Các vấn đề liên quan