is it possible to let a specific gesture fail so the next possible gesture is recognized?
to be more specific, look at the sample snippet:
UISwipeGestureRecognizer *swipeLeft = [initialize UISwipeGestureRecognizer... #selector(handleSwipe:)]
swipeLeft = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delegate = self;
UIPanGestureRecognizer *pan = [initialize UIPanGestureRecognizer... #selector(handlePan:)]
pan.delegate = self;
[pan requireGestureRecognizerToFail:swipeLeft];
the above code states that if swipe left is not recognized by the device, pan gesture handler will be used.
so my question: is it possible to let swipeLeft intentionally fail (after being recognized as swipe left touch by the device) based on some criteria that is checked on handleSwipe, and let the pan gesture handle the touch input instead?
thanks.
Check out the UIGestureRecognizerDelegate protocol here:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIGestureRecognizerDelegate_Protocol/Reference/Reference.html
Specifically, the
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
method might be useful. If you simply return YES from this method, both gestures can be recognized at the same time, so you can respond properly to both.
Assuming you have some other handler implemented for the pan gesture, can't you just do:
-(void)handleSwipe:(id)sender {
if //criteria is met to ignore left swipe
{
[self handlePan:self];
}
}
-(void)handlePan:(id)sender {
// handle pan gesture here
}
Related
I'm working on a GLKViewController based game which interprets taps and swipes as game controls. I want to support two-player mode by letting the first player tap or swipe on the left side of the screen, and letting the second player tap or swipe on the right side of the screen. In a perfect world, I'd like the gesture recognizers to work even if the swipes are sloppy and go past the centerline of the screen (with the starting point of the swipe being used to determine which player gets the input).
What would be the best way to implement this? Can I lay down a gesture recognizer on the left half of the screen, and another one on the right side of the screen? Will two separate recognizers work properly together even if both sides are being tapped/swiped rapidly at the same time? Or should I create a full-screen recognizer and parse the swipes and taps entirely on my own? I don't have experience with gesture recognizers so I don't know what the preferred approach is or how well they work when you have more than one being swiped on simultaneously.
I ended up making two UIViews overlaid on top of my GLKView, one on the left side of the screen and one on the right. Each view has a UIPanGestureRecognizer and a UILongPressGestureRecognizer (the long-press recognizer is basically a more flexible tap—I needed to use it to reject some gestures from being interpreted both as a pan and a tap at the same time). This worked magnificantly.
- (void)viewDidLoad
{
[super viewDidLoad];
// Add tap and pan gesture recognizers to handle game input.
{
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleLeftSidePan:)];
panRecognizer.delegate = self;
[self.leftSideView addGestureRecognizer:panRecognizer];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLeftSideLongPress:)];
longPressRecognizer.delegate = self;
longPressRecognizer.minimumPressDuration = 0.0;
[self.leftSideView addGestureRecognizer:longPressRecognizer];
}
{
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightSidePan:)];
panRecognizer.delegate = self;
[self.rightSideView addGestureRecognizer:panRecognizer];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightSideLongPress:)];
longPressRecognizer.delegate = self;
longPressRecognizer.minimumPressDuration = 0.0;
[self.rightSideView addGestureRecognizer:longPressRecognizer];
}
}
- (void)handleLeftSidePan:(UIPanGestureRecognizer *)panRecognizer
{
[self handleGameScreenPan:panRecognizer withVirtualController:&g_iPadVirtualController[0]];
}
- (void)handleRightSidePan:(UIPanGestureRecognizer *)panRecognizer
{
[self handleGameScreenPan:panRecognizer withVirtualController:&g_iPadVirtualController[1]];
}
- (void)handleLeftSideLongPress:(UILongPressGestureRecognizer *)longPressRecognizer
{
[self handleGameScreenLongPress:longPressRecognizer withVirtualController:&g_iPadVirtualController[0]];
}
- (void)handleRightSideLongPress:(UILongPressGestureRecognizer *)longPressRecognizer
{
[self handleGameScreenLongPress:longPressRecognizer withVirtualController:&g_iPadVirtualController[1]];
}
I need to animate view translation from A to B on the screen.
However, I want that translation to occur when a user swipes his thumb on the screen.
I also want translation to depend on the thumb swipe in a way that they follow one another.
I assume I will need some sort of listener, which will follow my thumb motion on the screen and I would somehow tell my view to move on the screen left or right, depending on the direction of the swipe.
How can I achieve that?
I would use the UIPanGestureRecognizer, because it has more control, for example if you move slow, it can still pickup where your movement is, then maybe you can position your translation accordingly.
You can do something like:
var panRecongniser = UIPanGestureRecognizer(target: self, action: Selector("didPanRecongnised:"))
in your viewDidLoad, and then:
func didPanRecongnised(recongniser: UIPanGestureRecognizer){
if(recongniser.state == UIGestureRecognizerState.Changed || recongniser.state == UIGestureRecognizerState.Began){
self.didPanMove(recongniser)
}else if(recongniser.state == UIGestureRecognizerState.Ended || recongniser.state == UIGestureRecognizerState.Cancelled){
self.didPanEnd(recongniser)
}
}
and in your didPanMove:
func didPanMove(recongniser: UIPanGestureRecognizer){
if((self.view.superview) != nil){
if(recongniser.state == UIGestureRecognizerState.Began){
// save the original position
self._origPoint = self.view.frame.origin
}
// this is the translation of the last segment of the "move"
let trans = recongniser.translationInView(self.view.superview!)
// this is the velocity of the last segment of the "move"
let velocity = recongniser.velocityInView(self.view.superview!)
}
}
Use the translation values to workout which direction the user is swiping. Don't forget to add the "trans" value as the accumulated translation each time "didPanMove" is called.
You can now use those value to do a number of things. Like having the appearing view to follow the progress of your finger. Or when the velocity reaches certain speed, having the view "snap" to the required position, like with a swipe. And maybe if the user don't swipe fast enough or far enough, having the view follow the finger a bit, then "snap" back to the hiding position when the gesture ends.
To do the animation, if you're doing a custom sliding out view, you can probably use UIView.animateWithDuration to animate your view sliding out.
I hope it helps you...
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(tappedRightButton:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(tappedLeftButton:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeRight];
If you start pressing the screen, but move the finger because minimumPressDuration ellapses, the gesture gets cancelled and your movement gets forwared to the view. If minimumPressDuration is reached, it doesn't matter how much you move the finger. I want to avoid this and always cancel my gesture if the finger movement is bigger than allowableMovement.
I've seen this thread, but that solution isn't working for me.
I've tried subclassing UILongPressureGestureRecognizer, and set the state to failed or cancelled when my requirement is met, but doesn't seem to work, I guess cancelling it isn't enough and have to forward the events myself? How should I do this? My intention is to use it together with MKMapView. I'm getting really frustrated with this, I've tried it for two days.
Try this one :
UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(LongPress_action:)];
longpress.minimumPressDuration = 2.0; //seconds
longpress.delegate = self;
[yourview addGestureRecognizer:longpress]; // where you want to add
Call this method :
- (void)LongPress_action:(UILongPressGestureRecognizer*)gesture
{
if ( gesture.state == UIGestureRecognizerStateEnded )
{
NSLog(#"Long Press");
}
}
Throughout iOS 7 there's many situations where users can slide their finger in from the left or right edge of the screen in order to perform an action, such as popping a view controller or showing a sidebar.
Is there a built in way to do this that I've completely overlooked somehow (yes, I've searched extensively)? Or is the only way to check the frame position of where the pan started?
This is because I want to perform distinct actions if the user pulls from the edge, or say the middle.
You have UIScreenEdgePanGestureRecognizer, which is added to iOS7 to detect, well, panning from the edges of the screen. For panning from the middle, a normal pan gesture recognizer will suffice, where you can check if the pan gesture originated close enough to the middle.
Use the UIScreenEdgePanGestureRecognizer, but check that it's available first (since it's iOS 7+):
if (NSClassFromString(#"UIScreenEdgePanGestureRecognizer")) {
UIScreenEdgePanGestureRecognizer *panRecognizer =
[[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self
action:#selector(handleScreenEdgePanGesture:)];
panRecognizer.edges = UIRectEdgeLeft;
}
Set up gestures
UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanning:)];
[self.view addGestureRecognizer:panGesture];
Handling gesture states
- (void)handlePanning:(UIPanGestureRecognizer *)gestureRecognizer
{
switch ([gestureRecognizer state])
{
case UIGestureRecognizerStateBegan:
[self startDragging:gestureRecognizer];
break;
//u won't need following cases
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateFailed:
[self stopDragging:gestureRecognizer];
break;
default:
break;
}
}
Recognizing start point of drag
- (void)startDragging:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint pointInSrc = [gestureRecognizer locationInView:yourVIEW];
}
The pan gesture recognizer is a predefined recognizer, about all you can do with it is to determine how fast the user moved their finger. If you want to tell if where the movement started, you'll have to code your own recognizer. It's not that difficult, you'll be notified when the touch started and where it ended.
In my app I have added the new Gesture Recognizers that are available in the 3.2 SDK. Everything appears to be working correctly and the response time on the screen been very fast. But for some reason when I add requireGestureRecognizerToFail to some of my gestures, there is a very visible delay when the gesture is triggered. Below is a snippet of the code that I use to create the Gesture Recognizers. Does anyone know why there is a delay and how I can fix it? I'm using requireGestureRecognizerToFail to prevent the SingleTap gesture from triggering when the user performs a DoubleTap.
- (void)createGestureRecognizers {
//Single Finger Double-Tap
UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleDoubleTap:)];
singleFingerDTap.numberOfTapsRequired = 2;
[super addGestureRecognizer:singleFingerDTap];
//Single Finger Tap
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleTap:)];
singleFingerTap.numberOfTapsRequired = 1;
[singleFingerTap requireGestureRecognizerToFail:singleFingerDTap];
[self addGestureRecognizer:singleFingerTap];
//Two Finger Pan
UIPanGestureRecognizer *panGesture2 = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePanGesture2:)];
panGesture2.maximumNumberOfTouches = 2;
[super addGestureRecognizer:panGesture2];
//Single Finger Pan
UIPanGestureRecognizer *panGesture1 = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePanGesture1:)];
panGesture1.maximumNumberOfTouches = 1;
[panGesture1 requireGestureRecognizerToFail:panGesture2];
[super addGestureRecognizer:panGesture1];
[singleFingerDTap release];
[singleFingerTap release];
[panGesture1 release];
[panGesture2 release];
}
If you want to distinguish between a single and double tap, you must wait long enough to figure out that no second tap is coming before you can call it a single tap. The alternative would be to design all your single tap actions in such a way that they can asynchronously be canceled or reverted when a double tap is detected.
For example, if you have a single tap change pages and a double tap zoom, then you would have to animate a page changing on single tap, then reverse the animation and zoom instead when a second tap is detected. By then the view that handled the single tap may have moved. In most cases, that is more trouble and confusion then it is worth.