问题
I'm developing a new app and i need to implement a functionality used very often in many apps. I want to implement the functions "next page" / "previous page" with a sliding gesture respectively from left to right for the "next page" case and from right to left in the other. I've seen something about GestureRecognizer that maybe can help me, but unfortunately i'm developing under 3.1.2 firmware version and it's not supported yet. Any suggestion or link with any tutorial ?
Thank you
回答1:
To answer your question with some code, this is my version of a great example, given in this thread.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = touches.anyObject;
//Define "CGPoint startTouchPosition;" in your header
startTouchPosition = [touch locationInView:self];
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = touches.anyObject;
CGPoint currentTouchPosition = [touch locationInView:self];
// If the swipe tracks correctly.
double diffx = startTouchPosition.x - currentTouchPosition.x + 0.1;
double diffy = startTouchPosition.y - currentTouchPosition.y + 0.1;
//If the finger moved far enough: swipe
if(abs(diffx / diffy) > 1 && abs(diffx) > 100)
{
if (!swipeHandled) {
[self respondToSwipe];
//Define "bool swipeHandled;" in your header
swipeHandled = true;
}
}
[super touchesMoved:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
swipeHandled = true;
[super touchesEnded:touches withEvent:event];
}
回答2:
Take a look at my code :
UISwipeGestureRecognizer *swipeRecognizer = [ [ UISwipeGestureRecognizer alloc ] initWithTarget:self action:@selector(myFunction) ]; [ swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionRight ]; [ view addGestureRecognizer:[ swipeRecognizer autorelease ] ];
You can change the direction of the swipe etc ;-)
Edit : oh I didn't see the end of your question :p So you should implement an UIView and detect the touchesBegan and touchesEnd, save CGPoint began and end and decide if it's a swipe or nop ;)
来源:https://stackoverflow.com/questions/3767404/iphone-slide-movement-recognizer