2010-05-03 17 views

cevap

20

Doğru yoldasın. DokunmalardaBegan, kullanıcının ekrana ilk dokunduğu yeri saklamanız gerekir.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    self.startPosition = [touch locationInView:self]; 
} 

Dokunuşlardanduğunda benzer kod, son konumu verir. İki pozisyonu karşılaştırarak hareket yönünü belirleyebilirsiniz. Eğer x-koordinatı belli bir toleransın ötesine geçtiyse, sol ya da sağa kaydırmaya sahip olursunuz.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    CGPoint endPosition = [touch locationInView:self]; 

    if (startPosition.x < endPosition.x) { 
     // Right swipe 
    } else { 
     // Left swipe 
    } 
} 

algılayabilir ve kullanıcının hala ekrana dokunarak iken bir tokatlamak izlemek istediğiniz sürece touchesMoved gerekmez. Ayrıca, bir tokatlama gerçekleştirdiklerine karar vermeden önce kullanıcının asgari bir mesafeye taşındığını test etmeye de değer olabilir.

+0

quixoto haklı senin sorun için bu iyi bir çözüm, daha iyi olduğunu düşünüyorum. – fyasar

+0

UITableview için kullanabilir miyim? – Mashhadi

+0

Bu oldukça modası geçmiş, 'UISwipeGestureRecognizer' kullanmak çok daha temiz (iOS 9) – Cbas

15

iPhone OS 3.2 veya sonraki sürümünü (tüm iPad'ler veya güncellenen iPhone'lar) hedeflemek istiyorsanız, UISwipeGestureRecognizer nesnesini kullanın. Bu önemsizce, ki bu da harikulade soğuk olacak.

36

Sen

UIView olarak Swipe dokunun ve yönünü saptamak veya herhangi bir yerde iphone sdk için UISwipeGestureRecognizer Nesne kullanarak olacaktır.

UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)]; 
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight; 
[rightRecognizer setNumberOfTouchesRequired:1]; 

//add the your gestureRecognizer , where to detect the touch.. 
[view1 addGestureRecognizer:rightRecognizer]; 
[rightRecognizer release]; 

UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)]; 
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; 
[leftRecognizer setNumberOfTouchesRequired:1]; 

[view1 addGestureRecognizer:leftRecognizer]; 
[leftRecognizer release]; 

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
{ 
    NSLog(@"rightSwipeHandle"); 
} 

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
{ 
NSLog(@"leftSwipeHandle"); 
} 

Ben GestureRecognizer yerleşik

+0

Paylaştığınız için teşekkürler, işe yarıyor. – Signcodeindie

İlgili konular