Multiple visible views responding to one gesture recognizer - ios

I have a view controller that consists of three views (self.panedview, self.view, self.sineview) When a swipe up gesture is detected, the highest view (self.panedview) is moved up halfway - revealing two additional views (self.view and self.sineview). self.sineview is a UIView that constantly has an animation running that renders a moving sinewave and takes up half of self.view. I have a swipe down gesture recognizer that works when I swipe down on self.panedview, but doesn't work when I swipe down on self.sineview. If I swipe around self.sineview on self.view it seems to work. When I hide self.sineview and swipe directly down on either self.view or self.paned view, the swipe down works. do you think the animating sine wave gets in the way of the gesture recognition.
UISwipeGestureRecognizer * swipeDownRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleDownSwipe:)];
[self.panedView addGestureRecognizer:swipeDownRec];
[self.view addGestureRecognizer:swipeDownRec];
[self.sineview addGestureRecognizer:swipeDownRec];
Also I tried varying between these two lines of code but there is no difference:
[self.view insertSubview:self.sineWave belowSubview:self.panedView];
[self.view insertSubview:self.sineWave aboveSubview:self.view];
I also tried adding a separate swipe down gesture recognizer for each view, but it still doesn't work.

The problem was the that the swipe recognizer for self.sinewave couldn't be recognized while the self.sinewave animation was enabled. The solution is simple: add UIViewAnimationOptionAllowUserInteraction as a parameter to the options handler for animateWithDuration:delay:options:animations:completion:

Related

How to detect long press within a view and its subviews swift

I have a view (I'll call it parentView) that has about 20-30 subviews. I have added a long press gesture recognizer to the parentView. The gesture recognizer only seems to fire when I press on the parentView, it does not fire when I press and hold on one of the subviews.
I have tried adding the gesture recognizer to self.view and using the gesture location to see if it was within the bounds of the parentView. However the same problem occurs since it does not seem to detect my long press on the subviews.I have also tried running a for loop and adding the gesture recognizer to each individual subview but this also did not work.
This is how I am defining my gesture recognizer if anybody was wondering.
longPress.minimumPressDuration = 1
longPress.addTarget(self, action: #selector(ViewController.handleLongPress)
parentView.addGestureRecognizer(longPress)
How would I get the long press gesture recognizer to detect a long press on the parentView and it's subviews?

iOS: right swipe gesture not responding in UIPageViewController

I am currently facing a scenario that I am using a UIPageViewController on a view & I want the right swipe gesture to work when user swipe right side from index value 0, so I am trying to add a right swipe gesture to UIPageViewController by the following code:
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
//Edit 1
[self.pageViewController.view addGestureRecognizer:rightRecognizer];
but, while I am adding gesture to pageviewcontroller I am getting following error message (Solved by #Lyndsey Scott answer)
no visible interface uipageviewcontroller declares the selector addgesturerecognizer
Can anyone guide me that how can I implement the right swipe in this pageViewController
Edit 1: Replaced the code provided by #Lyndsey Scott, removed the error, but problem still exists that I am not able to trigger that swipe event.
You have to the gesture to the UIPageViewController's view:
[self.pageViewController.view addGestureRecognizer:rightRecognizer];
Edit in response to your edit:
The swipe gesture won't work if you haven't implemented any UIGestureRecognizerDelegate methods to allow for the UIPageViewController's swipe gesture and your swipe gesture to be recognized simultaneously. Right now, your swipe is essentially being blocked by the page controller's gesture. You could change that by implementing the UIGestureRecognizerDelegate, setting rightRecognizer.delegate = self;, and overriding gestureRecognizer: shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer such that it returns true in the case that both gesture recognizers are triggered.
But if you want the page to flip and your gesture to be recognized at the time of that page flip, adding the extra swipe gesture is unnecessary since you can simply trigger your method upon page turn; for example in pageViewController:didFinishAnimating:previousViewControllers: transitionCompleted:.
You shouldn't have to add the gesture recognizer, if you implement the protocol correctly it should work out of the box.
The point of UIPageViewController is to not have to do it manually.

Give swipe gesture priority over button

I have two UIButtons in a view (one is YES, the other NO). I now want to add a "did not answer", which would be indicated by a downward swipe on the view.
The problem is that the user may swipe down on the view, but in the process hit one of the buttons. When this happens I want to ignore the button press if there was a swipe underway. If it is just a tap on a button, the answer is recorded.
So, if the swipe occurs, I want to call the swipe gesture's action method. If it is determined no swipe occurred but one of the two buttons was touched I want to call their respective action methods. But if a button was touched in the process of a swipe, I want to call only the swipe gesture's action method.
I know there is a way but I'm wondering whether there is an EASY way of doing this. TIA for suggestions.
Since UISwipeGestureRecognizer is a "discrete" gesture, it just triggers a single action when recognized and it won't allow you to detect the end of the gesture.
So to prevent other touches during the gesture, I'd recommend using a UIPanGestureRecognizer instead since it can track your gesture from beginning to end. Then you can try setting your gesture's cancelsTouchesInView property to YES to cancel all other touches in the view that happen while that pan gesture is recognized, ex:
gesture.cancelsTouchesInView = YES;
gesture.delaysTouchesBegan = YES;
Your buttons should be registering touch up in view, so that someone who taps on the button can drag off if they decide not to proceed.
For your other swipe gesture, your buttons will not register a touch up in view during a swipe gesture, even if the swipe passes over the button or ends on it.
Okay, this seems to work. On the gesture recognizer, I used:
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipeDown:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
swipeDown.delaysTouchesBegan = YES;
swipeDown.delegate = self;
[self addGestureRecognizer:swipeDown];
They delaysTouchesBegan = YES allows it to determine whether the swipe has occurred before passing the touches on to the buttons. So, if you swipe, it calls the swipe GR, and if you touch either button, you get that. Thanks for your answers...

Moving view but keeping imageview in fixed position

I'm moving a UIView up with a swipe, but I want the imageView within the UIView to stay in its position. I got this working by placing my imageView under a separate UIView ( I needed to include a label under the imageview) by doing [self.view insertSubview:self.imageViewHolder aboveSubview:self.panedView];. The problem now is that when i swipe down over the imageView, my view that I swiped up won't come back down unless I swipe around the imageView. This is because the UIGestureRecognizer is associated with the view under the imageView. Any simple ways around this?
Gesture recognizer code:
UISwipeGestureRecognizer * swipeUpRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleUpSwipe:)];
[self.panedView addGestureRecognizer:swipeUpRec];
[self.imageViewHolder addGestureRecognizer:swipeUpRec];

Swipe gesture in UIButton

I have a UIView at the bottom of my screen with a UIButtonon top, showing, when I press the button, view slides up with a nice animation.
Everything works fine, now I would like to enable swipe up gesture in the UIButton, so user doesn't actually have to press the button but just slide the finger.
To slide up the view, I just set the frame of it in button action. Now, would be also nice to swipe the view with the finger, something like when you release the view goes up or down, but if you don't release the swipe, the view stays at your finger. Does that make sense?
How could I achieve this?
[UIView animateWithDuration:1.0 animations:^{
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
self.sliding.view.frame = CGRectMake(0, 418, 320, 150);
}];
Do you want to swipe the view, or swipe the button?
In either case, what you would do would be to create and configure a UISwipeGestureRecognizer, and attach it to the field that you want to respond to the swipe gesture.
I would suggest attaching the gesture recognizer to the view that's being moved, not to the button. I think it would be confusing to swipe on a button and have it slide a view to the side as a result.
Take a look at the docs on UISwipeGestureRecognizer in Xcode. You'll want to set the direction and number of touches required.
You'll want to create a swipe gesture recognizer with initWithTarget:action:, then attach it to the view with the UIView method addGestureRecognizer:. If you are attaching the gesture recognizer to a non-control, you might need to set the userInteractionEnabled flag on the view to YES.
In order to achieve this, you need to use swipe gesture recognizers.
UISwipeGestureRecognizer object is will help you to swipe movement with your fingers in any way.
It's include left and right direction due to understand the user which way to swipe .

Resources