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.
Related
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...
I have a longpress gesture recognizer that I create in ViewDidLoad then attach to a button like this, the button is created in the storyboard and linked to my class.
UILongPressGestureRecognizer *hold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(secretChange:)];
hold.minimumPressDuration = 5.0;
hold.delegate = self;
[_button addGestureRecognizer:hold];
The class conforms to the GestureRecognizer protocol and I have my selector here
- (void)secretChange:(UILongPressGestureRecognizer *)sender {
// Some stuff
NSLog(#"Secret");
}
The selector is not being called and I cannot figure out why, this seems to be the code everyone gives out on the internet, I have tried removing the minimum duration to make sure I didn't accidentally set it ridiculously long
UPDATE: I am actually adding this gesture recognizer to multiple buttons like this
[_button1 addGestureRecognizer:hold];
[_button2 addGestureRecognizer:hold];
[_button3 addGestureRecognizer:hold];
What is happening is the gesture recognizer is only being applied to the last button I add it to. How do I get the gesture recognizer added to ALL the buttons? Do I need to make a new one for every button?
You should have three instance of UILongPressGestureRecognizer.
Before add a gesture recognizer to a new view, the addGestureRecognizer method will remove the gesture recognizer from the view it has been attached to.
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:
I'm trying to add a very simple swipe up gesture to one of my views. The gesture does not work at all unless I apply an outward/opposite pinch gesture with two of my fingers. I am creating the gesture connections via IB. I have set up the gesture properties to Swipe: Down and Touches: 1. However it will only work with a 2 finger outward pinching motion? What is going on? Thanks.
Edit:
Ok, I've tried coding it out rather than using interface builder and I'm getting the same results. Here is the code I'm using
UISwipeGestureRecognizer *swipeUpOnVideoView = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(SwipeUp:)];
swipeUpOnVideoView.direction = UISwipeGestureRecognizerDirectionUp;
swipeUpOnVideoView.numberOfTouchesRequired = 1;
[self.playerView addGestureRecognizer:swipeUpOnVideoView];
As you can see I am specifically designating only 1 touch, however my SwipeUp: method will only execute if I use an outward pinching motion with two fingers???
check this it working proper in my application:
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeUp:)];
[swipeUp setDirection:UISwipeGestureRecognizerDirectionUp ];
[self.view addGestureRecognizer:swipeUp];
- (IBAction)swipeUp:(UISwipeGestureRecognizer *)recognizer
{
// do you stuff here
}
May be it will help you.
Happy coding...:)
It turned out I had a number of other UITapGestureRecognizers that were on my Xib file. Even though I wasn't calling them in the code anywhere they were associated with views on my page via the connections inspector. For some reason this interference made it so that my single finger swipeGesture would only register if I used a two finger pinching motion.
Deleting multiple UITapGestureRecognizers from my xib and then writing the code for the UISwipeGestureRecognizer programmatically fixed the problem for me.
I am overriding my horizontal image UIScrollView with a panning gesture recognizer to detect a user swipe.
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panGestureCaptured)];
[imgHorizontalScrollView addGestureRecognizer:panGesture];
My question is: Is there a way for me to reset imgHorizontalScrollView's pan gesture recognizer back to default in the panGestureCaptured method? The reason I ask is because since I am overriding this gesture, once the user swipes and the gesture is picked up, I am no longer able to scroll in the scroll view. I also tried to remove the gesture but that also prevents me from being able to scroll.
try UIGestureRecognizerDelegate gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: with scrollView's gestureRecognizer got by panGestureRecognizer
Try setting
panGesture.cancelsTouchesInView = NO;
If that doesn't work, you should find a way to change your gesture handler to the scrollViewDidScroll: delegate method