I am trying to detect a two finger vertical swipe, and then take an action. Specifically I want to fade an image in, or out, depending on its current state. I can get it to work, but it is very unreliable. Sometimes it will work a few times in a row. Other times I might swipe 10 times before it works, even though I'm making the same motion (length, speed, area on screen).
User Interaction and Multiple Touch are both enable for the items in Interface Builder. My related controller.h code
Code:
// swipes
#define kMinimumGestureLength 25
#define kMaximumVariance 5
typedef enum {
kNoSwipe = 0
,kHorizontalSwipe
,kVerticalSwipe
} SwipeType;
@interface LifeForceViewController : UIViewController <UIAccelerometerDelegate, UIActionSheetDelegate> {
//swipes
CGPoint gestureStartPoint;
}
//swipes
@property CGPoint gestureStartPoint;
my related controller.m code
Code:
//swipes
@synthesize gestureStartPoint;
// swipes
#pragma mark -
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:self.view];
}
// swipes
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.view];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
/* commented out because detecting vertical swipe only
if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {
// handle horizontal swipe
}
else */
if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) {
// handle vertical swipe
if ([touches count] == 2)
{
if (imageView.alpha == 0)
// method to fade in image
[self imageIn];
}
else
{
// method to fade out image
[self imageOut];
}
}
}
I have been
through the forum, and the
source code from the Beginning iPhone Development Book, but am not finding a way to resolve this. Any insight is appreciated.
PS-I had a touchesEnded event that is used to switch from my info view back to the main screen. I thought maybe it was interfering, but commented it out and no change.