Reputation: 3165
In ios 3.1 and above how I can detect the Gesture in an UIView...
In ios >= 3.2.3 I use this code...(for example):
UISwipeGestureRecognizer *oneFingerSwipeLeft =
[[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeLeft:)] autorelease];
[oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:oneFingerSwipeLeft];
UISwipeGestureRecognizer *oneFingerSwipeRight =
[[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeRight:)] autorelease];
[oneFingerSwipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:oneFingerSwipeRight];
Someone have an idea/example...
Thanks
Upvotes: 0
Views: 343
Reputation: 100632
You're going to have to subclass the UIView
(or implement the stuff within the view controller if the layout isn't too complicated) and track the gestures you want for yourself using ye olde UIResponder
methods touchesBegan:withEvent:
, etc.
For example:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if(!trackingTouch && [touches count] == 1)
{
trackingTouch = [touches anyObject];
startingPoint = [trackingTouch locationInView:relevantView];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(trackingTouch && [touches containsObject:trackingTouch])
{
CGPoint endingPoint = [trackingTouch locationInView:relevantView];
trackingTouch = nil;
if(endingPoint.x < startingPoint.x)
NSLog(@"swipe left");
else
NSLog(@"swipe right");
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
// don't really care about touchesMoved:withEvent:
That's an imperfect solution because it assumes that all finger down to finger up progressions are necessarily swipes. You'll probably want to implement some sort of maximum time duration or track velocities in touchesMoved:withEvent:
or check that the touch moved at least a minimum distance. I think it's because people were making different decisions for all of those sorts of things that Apple ended up providing the UIGestureRecognizer
s.
Upvotes: 2