2013-05-17 59 views
34

Tôi muốn nhận vị trí của UITouch từ vòi của tôi từ UIGestureRecognizer, nhưng tôi không thể tìm ra cách nhìn vào cả tài liệu và các câu hỏi SO khác. Bạn có thể hướng dẫn tôi không?Cách lấy vị trí UITouch từ UIGestureRecognizer

- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer 
{ 

CCLOG(@"Single tap"); 
UITouch *locationOfTap = tapRecognizer; //This doesn't work 

CGPoint touchLocation = [_tileMap convertTouchToNodeSpace:locationOfTap]; 
//convertTouchToNodeSpace requires UITouch 

[_cat moveToward:touchLocation]; 

} 

MÃ CỐ ĐỊNH TẠI ĐÂY - này cũng sửa chữa Inverted Y AXIS

CGPoint touchLocation = [[CCDirector sharedDirector] convertToGL:[self convertToNodeSpace:[tapRecognizer locationInView:[[CCDirector sharedDirector] openGLView]]]]; 

Trả lời

68

Bạn có thể sử dụng phương pháp locationInView: trên UIGestureRecognizer. Nếu bạn vượt qua nil cho khung nhìn, phương thức này sẽ trả về vị trí của liên lạc trong cửa sổ.

- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer 
{ 
    CGPoint touchPoint = [tapRecognizer locationInView: _tileMap] 
} 

Ngoài ra còn có phương thức đại biểu hữu ích gestureRecognizer:shouldReceiveTouch:. Chỉ cần đảm bảo triển khai và đặt ủy nhiệm của cử chỉ nhấn của bạn thành tự.

Giữ tham chiếu đến trình nhận dạng cử chỉ.

@property UITapGestureRecognizer *theTapRecognizer; 

Initiailze cử chỉ recognizer

_theTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(someMethod:)]; 
_theTapRecognizer.delegate = self; 
[someView addGestureRecognizer: _theTapRecognizer]; 

Nghe cho các phương pháp đại biểu.

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{ 
    CGPoint touchLocation = [_tileMap convertTouchToNodeSpace: touch]; 
    // use your CGPoint 
    return YES; 
} 
+0

Tôi cần một UITouch cho tôi 'CGPoint touchLocation = [_tileMap convertTouchToNodeSpace: locationOfTap]; 'đâu convertTouchToNodeSpace chuyển đổi một UITouch .. có thể mã này được sử dụng theo cách đó? –

+0

Bạn có thể sửa đổi phương thức 'convertTouchToNodeSpace:' để lấy một CGPoint không? – MJN

+0

Tôi không thể tìm thấy nơi được xác định, hahaha –

1

Hãy thử điều này:

-(void) didMoveToView:(SKView *)view{ 
    oneFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTapDetected:)]; 
    oneFingerTap.numberOfTapsRequired=1; 
    oneFingerTap.numberOfTouchesRequired=1; 

    [view addGestureRecognizer:oneFingerTap]; 
} 

-(void)oneTapDetected:(UITapGestureRecognizer *)recognizer{ 
    NSLog(@"one tap detec"); 
    tapPositionOneFingerTap = [oneFingerTap locationInView:self.view]; 
    NSLog(@"%f, %f",tapPositionOneFingerTap.x,tapPositionOneFingerTap.y); 
} 

này in tọa độ của mỗi vòi trong giao diện điều khiển của bạn.

0

Apple Docs nói

UIGestureRecognizer

- (NSUInteger)numberOfTouches 

Số lượng đối tượng UITouch trong một mảng riêng duy trì bởi người nhận.

Vì vậy, bạn không nên truy cập chúng.

Sử dụng giá trị được trả về bằng phương pháp này trong vòng lặp, bạn có thể yêu cầu vị trí của từng lần chạm bằng phương pháp locationOfTouch:inView:.

10

Trong Swift:

func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) { 
    print("tap working") 
    if gestureRecognizer.state == UIGestureRecognizerState.Recognized 
    { 
     print(gestureRecognizer.locationInView(gestureRecognizer.view)) 
    } 
}