2011-07-05 29 views
14

Tôi đang cố sử dụng vuốt sang trái và sang phải trên UIScrollView. Tuy nhiên có vẻ như swipe trái không hoạt động trong mô phỏng iPhone mặc dù swipe phải không. Tôi có bỏ lỡ bất kỳ bước nào không?Tác vụ vuốt micrô mô phỏng của iPhone, nhưng vuốt sang trái không?

 
- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    self.scrollView.multipleTouchEnabled = YES; 
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
    swipe.delaysTouchesBegan = YES; 
    swipe.numberOfTouchesRequired = 2; 
    [self.scrollView addGestureRecognizer:swipe]; 
    [swipe release]; 
} 

- (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer 
{ 
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) { 

    } else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) { 

    } 
} 

Trả lời

19

Sử dụng sau:

UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)]; 
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight; 
[rightRecognizer setNumberOfTouchesRequired:1]; 
[mainSlideShowImageScrollView addGestureRecognizer:rightRecognizer]; 
[rightRecognizer release]; 
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)]; 
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; 
[leftRecognizer setNumberOfTouchesRequired:1]; 
[mainSlideShowImageScrollView addGestureRecognizer:leftRecognizer]; 
[leftRecognizer release]; 



- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
    { 
     //Do moving 
    } 

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
{ 
    // do moving 
} 
+0

Bạn có nghĩa là tạo ra 2 UISwipeGestureRecognizers, một với selector rightSwipeHandle :, other với leftSwipeHandle:? Tôi vẫn cần phải kiểm tra hướng nhận dạng bên trong 2 trình xử lý đó? –

+0

Vâng tôi có ý như vậy. Và bạn không cần phải bận tâm cho bất cứ điều gì khác, chỉ với những gì bạn muốn làm, trong swipe trái hoặc phải. Nhưng vâng, đừng bỏ lỡ mã được viết ban đầu, vì nó nhận dạng cử chỉ đến một vùng chứa cụ thể. – rptwsthi

+0

Chỉ cần nhận thấy rằng tôi có thể thiết lập UISwipeGestureRecognizer.direction trong viewWillAppear. Vuốt sang trái hoạt động sau khi tôi đặt hướng đến UISwipeGestureRecognizerDirectionLeft. Bây giờ tôi hiểu ý bạn là gì. Cảm ơn nhiều. –

4

mã 'handleSwipe' của bạn là thực sự chính xác. Bạn cần hai UISwipeGestureRecognizers nhưng bạn có thể trỏ cả hai vào cùng một trình xử lý, chứa câu lệnh IF của bạn.

0

Bạn có thể tạo một trình nhận dạng cử chỉ để xử lý cả hai swipe trái và phải (hoặc thậm chí tất cả Lượt chỉ!):

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
     swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight; 

Tất cả các hướng:

swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown; 
Các vấn đề liên quan