2012-03-14 29 views
6

Tôi thiếu một cái gì đó đơn giản ở đây, xin vui lòng giúp tôi hiểu những gì.Thiết lập SwipeGestureRecognizer, làm rõ cần thiết

điều khiển của tôi lập

UIGestureRecognizer *swipe = [[UIGestureRecognizer alloc] 
      initWithTarget:gameView action:@selector(handleSwipeFrom:)]; 
[gameView addGestureRecognizer:swipe]; 

Các GameView được thiết lập

@interface GameView : UIView<UIGestureRecognizerDelegate> 

Nó tiếp tục tuyên bố

- (IBAction) handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer; 

và đặt nó lên như

- (IBAction)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer { 
    NSLog(@" .............. Swipe detected!! ..................."); 
} 

Các Storyboard liên kết UIGestureRecognizer với điều này IBACtion và được thiết lập như sau: enter image description here

Tôi là một chút lo ngại rằng UIGestureRecognizer xuất hiện bên dưới một điều khiển và không phải là cái nhìn như thể hiện, nhưng tôi có thể dường như không thể sửa nó.

enter image description here

Khi ứng dụng được xây dựng, swipes, tuy nhiên không được công nhận.

Vui lòng đề xuất những gì tôi đang thiếu ở đây và liệu tôi có đang đi đúng cách để thiết lập mọi thứ không.

+0

Bạn đang initing một UIGestureRecognizer thay vì một UISwipeGestureRecognizer cho một – Brodie

Trả lời

16

Bạn đang thiết lập hai trình nhận dạng cử chỉ riêng biệt. Một trong mã mà có khả năng không có gì, người kia trong bảng phân cảnh có khả năng không có mục tiêu.

Trong mã bạn nên khởi tạo một swipe cử chỉ nhận dạng như sau:

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:gameView action:@selector(handleSwipeFrom:)]; 
// And assuming the "Up" direction in your screenshot is no accident 
swipe.direction = UISwipeGestureRecognizerDirectionUp; 

Hoặc, tất nhiên, bạn có thể kết nối các recognizer swipe trong kịch bản này. Điều này có thể dễ dàng thực hiện bằng cách nhấp chuột phải vào trình nhận dạng cử chỉ và kết nối SentActions với phương thức handleSwipeFrom: hoặc tương tự bạn có thể kéo từ trình kiểm tra kết nối ở bên phải (như được thấy trong ảnh chụp màn hình) đến hàm được đặt tên. Trong hình ảnh bạn có thể thấy tôi có Sent Actions được kết nối với phương thức swipeTarget:.

enter image description here

Nhưng hiện tại bạn có hai recognizers không đầy đủ.

+0

Tuyệt vời. Cảm ơn bạn rất nhiều NJones – JAM

+2

Không sao cả. Vui vẻ giúp đỡ. – NJones

1
UISwipeGestureRecognizer *swipe = [[UISwipGestureRecognizer alloc] 
     initWithTarget:gameView action:@selector(handleSwipeFrom:)]; 
    [gameView addGestureRecognizer:swipe]; 


- (IBAction)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer { 
    NSLog(@" .............. Swipe detected!! ..................."); 
} 

Sau đó, chỉ hoàn tác những gì bạn đã làm trong IB. Nếu bạn làm điều đó trong mã, sau đó làm điều đó trong IB quá bạn chỉ cần sao chép công việc, và có thể tăng gấp đôi lần so với swipe được xử lý

11
-(void)addSwipeEvent:(UIView*)subView{ 

    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; 
    recognizer.numberOfTouchesRequired = 1; 
    recognizer.delegate = self; 
    [subView addGestureRecognizer:recognizer]; 
    [recognizer release]; 

    UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; 
    leftRecognizer.direction=UISwipeGestureRecognizerDirectionLeft; 
    leftRecognizer.numberOfTouchesRequired = 1; 
    leftRecognizer.delegate = self; 
    [subView addGestureRecognizer:leftRecognizer]; 
    [leftRecognizer release]; 

    UISwipeGestureRecognizer *downRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; 
    downRecognizer.direction=UISwipeGestureRecognizerDirectionDown; 
    downRecognizer.numberOfTouchesRequired = 1; 
    donwRecognizer.delegate = self; 
    [subView addGestureRecognizer:downRecognizer]; 
    [downRecognizer release]; 

    UISwipeGestureRecognizer *upRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; 
    upRecognizer.direction=UISwipeGestureRecognizerDirectionUp; 
    upRecognizer.numberOfTouchesRequired = 1; 
    upRecognizer.delegate = self; 
    [subView addGestureRecognizer:upRecognizer]; 
    [upRecognizer release]; 
} 

- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender { 
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft){ 
     NSLog(@" *** SWIPE LEFT ***"); 

    } 
    if (sender.direction == UISwipeGestureRecognizerDirectionRight){ 
     NSLog(@" *** SWIPE RIGHT ***"); 

    } 
    if (sender.direction== UISwipeGestureRecognizerDirectionUp){ 
     NSLog(@" *** SWIPE UP ***"); 

    } 
    if (sender.direction == UISwipeGestureRecognizerDirectionDown){ 
     NSLog(@" *** SWIPE DOWN ***"); 

    } 
} 


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { 
    if ([touch.view isKindOfClass:[UIView class]]) 
    { 
     return YES; 
    } 
    return NO; 
} 
+1

triển khai UIGestureRecognizerDegegate delegate trong tệp .h. – Vibhooti

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