2011-04-19 30 views
10

Tôi có một số rắc rối để xử lý swipe trên iPhone, tôi tạo ra trong giao diện của tôi một var UISwipeGestureRecognizer:Swipe cử chỉ iphone

UISwipeGestureRecognizer *swipeRecognizer; 

và trong điều khiển của tôi: viewDidLoad phương pháp

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    // Horizontal swipe 
    swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                      action:@selector(swipeMethod:)]; 
    swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft; 
    [self addGestureRecognizer:swipeRecognizer]; 
} 

Và phương pháp của tôi để xử lý swipe là:

-(void)swipeMethod: (UISwipeGestureRecognizer *) sender 
{ 
    NSLog(@"Swipe!"); 
} 

Khi tôi chạy mã và d o một swipe tôi đã không có gì? Tôi phải nhận được: Swipe!

Cảm ơn.

+0

Không thêm nó vào chính bản thân, hoặc sử dụng self.view (giả sử bản thân là một bộ điều khiển xem) hoặc sử dụng chế độ xem phụ để đặt trình nhận dạng cử chỉ. Bạn có thể đặt điểm ngắt trước khi đặt trình nhận dạng và xem nó có được thêm chính xác hay không. Xem câu trả lời của tôi cho phát hiện swipe trái và phải: http://stackoverflow.com/a/16810160/936957 –

Trả lời

9

Tôi rất ngạc nhiên khi không gặp sự cố với lỗi bộ chọn không được công nhận. Thử thêm bộ nhận dạng quan điểm của bạn thay vì để điều khiển xem của bạn:

[self.view addGestureRecognizer:swipeRecognizer] 
+0

Cảm ơn, tôi ngạc nhiên quá, tôi thêm nó vào quan điểm của tôi, vẫn không có gì! – Lekhal

0

swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;

Tính năng này có hoạt động không?

Bạn không phải thêm hai gestureRecognizer cho mỗi hướng?

+0

Không. Chúng có thể là OR'ed. Nếu bạn 'Chuyển sang định nghĩa', bạn sẽ thấy điều này. 'Typedef NS_OPTIONS (NSUInteger, UISwipeGestureRecognizerDirection) { UISwipeGestureRecognizerDirectionRight = 1 << 0, UISwipeGestureRecognizerDirectionLeft = 1 << 1, UISwipeGestureRecognizerDirectionUp = 1 << 2, UISwipeGestureRecognizerDirectionDown = 1 << 3 }; ' – cnotethegr8

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