I'm detecting a swipe on the screen, and it works exactly the way I want it to. The only thing is, in testing, I keep swiping over and over and over again and at least 90% of the time or more it is responding, but every now and again there is no response.
I NSLog'd everything to find the culprit and found that touchesBegan isn't getting detected at all on the few times that this happens.
Here's my code, although touchesBegan isn't even getting called so the code shouldn't matter:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches began!");
UITouch *touch = [touches anyObject];
CGPoint thisTouch = [touch locationInView:self.view];
touchstartedX = thisTouch.x;
touchstartedY = thisTouch.y;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint p = [[touches anyObject] locationInView:self.view];
if ((abs(p.y - touchstartedY)) < 120) {
if ((p.x - 开发者_开发技巧touchstartedX) > 10) {
[self goPrevious];
} else if ((p.x - touchstartedX) < -10) {
[self goNext];
}
} else { NSLog(@"too much Y"); }
}
Any ideas?
Thanks!
* EDIT WITH SOLUTION * Here's the code I ended up using after exploring UISwipeGestureRecognizer on dredful's suggestion:
in .h:
UIViewController <UIGestureRecognizerDelegate>
UISwipeGestureRecognizer *swipeLeft;
UISwipeGestureRecognizer *swipeRight;
@property (nonatomic, retain) UISwipeGestureRecognizer *swipeLeft;
@property (nonatomic, retain) UISwipeGestureRecognizer *swipeRight;
in .m:
@synthesize swipeLeft, swipeRight;
UIGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
self.swipeLeft = (UISwipeGestureRecognizer *)recognizer;
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
self.swipeLeft = (UISwipeGestureRecognizer *)recognizer;
[recognizer release];
recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
self.swipeRight = (UISwipeGestureRecognizer *)recognizer;
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
self.swipeRight = (UISwipeGestureRecognizer *)recognizer;
[recognizer release];
[self.view addGestureRecognizer:swipeLeft];
[self.view addGestureRecognizer:swipeRight];
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
[self goNext];
} else {
[self goPrevious];
}
}
- (void)dealloc
{
[swipeLeft release];
[swipeRight release];
[super dealloc];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.swipeLeft = nil;
self.swipeRight = nil;
}
It is much easier to use Apple's UISwipeGestureRecognizer
// add Swipe Left
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
[swipeLeft release];
// add Swipe Right
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
[swipeRight release];
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
//do something
}
- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer {
//do something
}
精彩评论