I want to remove gesture recogniser for RESidemenu controller form particular UIView. how can we remove it.
i have removed all gestures from view, but its not working fine.
for (UIGestureRecognizer *gesture in self.view.gestureRecognizers)
{
[self.view removeGestureRecognizer:gesture];
}
Related
I've a scrollview on which one button is added, now I want to give action as well as double tap gesture to the button.
If the button is on UIView, both action of the button and double tap gesture methods work perfectly. But if the button is present on UIScrollView then action gets called followed by double tap gesture method.
Any help will be appreciated.
The scroll view needs a fair amount of touch logic to track, and that must be getting confused with the button's touch logic and your gesture recognizer.
Without investigating that further, I'd get around this by handling the button's taps via two gesture recognizers that you control, which should work no matter the parent view.
Do not give the button a target/action as you normally would in IB or in code
Create a single tap gesture recognizer to handle the button action.
Create a double tap gr as you probably do already. Add both to the button.
// for your current target-action behavior
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTap:)];
singleTap.numberOfTapsRequired = 1;
doubleTap.numberOfTapsRequired = 2;
[singleTap requireGestureRecognizerToFail:doubleTap];
[button addGestureRecognizer:singleTap];
[button addGestureRecognizer:doubleTap];
Then, instead of your IBAction method...
- (void)singleTap:(UIGestureRecognizer *)gr {
if (gr.state == UIGestureRecognizerStateRecognized) {
// target-action behavior here
NSLog(#"single tapped");
}
}
- (void)doubleTapped:(UIGestureRecognizer *)gr {
if (gr.state == UIGestureRecognizerStateRecognized) {
NSLog(#"double tapped");
}
}
I have a ViewController with a vertically scrolling collection view that takes up the entire view. I want to be able to get swipe and pan gestures on the entire collection view (not just on cells) but I can't get any gestures. I have tried adding the gesture recognizer to the view and the collection view but neither seem to work.
Adding the gesture recognizer to the view
self.panEdgeGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
self.panEdgeGesture.delegate = self;
[self.collectionView addGestureRecognizer:self.panEdgeGesture];
[self.panEdgeGesture setEdges:UIRectEdgeRight];
Then I added these functions:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch{
return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (void)handlePan:(UISwipeGestureRecognizer *)sender
{
DebugLog(#"Received pan gesture");
}
Could the collection view cells stop the gesture events from triggering? They have no gestures themselves.
Per UIScreenEdgePanGestureRecognizer's Class Reference:
After creating a screen edge pan gesture recognizer, assign an
appropriate value to the edges property before attaching the gesture
recognizer to your view. You use this property to specify from which
edges the gesture may start. This gesture recognizer ignores any
touches beyond the first touch.
So change you code to:
self.panEdgeGesture = [[UIScreenEdgePanGestureRecognizer alloc];
[self.panEdgeGesture setEdges:UIRectEdgeRight];
initWithTarget:self action:#selector(handlePan:)];
self.panEdgeGesture.delegate = self;
[self.collectionView addGestureRecognizer:self.panEdgeGesture];
I've made a custom UIView that appears in my app in two places. It first appears in a regular view controller along with a text view, and then a tap gesture on the UIView moves to another view controller where the custom UIView is shown enlarged. I want to have a different tap gesture for the enlarged UIView, but when I tried putting this into the storyboard, the first tap gesture stopped working (since the new tap gesture was only reachable via the first tap gesture's segue to the new view controller, I wasn't able to see if it worked or not.
Does adding the second UITapGestureRecognizer to the view cause the first to become invalid? I don't see any other reason this would've caused an issue. If so, do I need to create separate classes for the two different custom UIViews? I tried to avoid this since they're showing the same thing just in different sizes but it seems the different tap gestures may be causing an issue.
Adding different UITapGesture in different.
View1
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapBehind1:)];
[recognizer setNumberOfTapsRequired:1];
recognizer.cancelsTouchesInView = NO;
[View1 addGestureRecognizer:recognizer];
View2
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapBehind2:)];
[recognizer setNumberOfTapsRequired:1];
recognizer.cancelsTouchesInView = NO;
[View2 addGestureRecognizer:recognizer];
View1 handle Tap method
- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
[View1 removeGestureRecognizer:sender];
}
}
View2 handle Tap method
- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
[View2 removeGestureRecognizer:sender];
}
}
I am using SWRevealViewController from John-Lluch. I need to use pan gesture to view the sidebars and I am using swipe to view my previous and next articles. However, the pan gesture can only be detected.
UPDATED: My swipe gesture worked if I disable my pan gesture.
Pan Gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
Swipe gesture
UISwipeGestureRecognizer *left = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft:)] autorelease];
left.direction = UISwipeGestureRecognizerDirectionLeft;
UISwipeGestureRecognizer *right = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight:)] autorelease];
right.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:left];
[self.view addGestureRecognizer:right];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
[self.view addGestureRecognizer:left];
Notice the difference? Implement the SwipeGesture as a property in your .h or make it in your .xib and link it to your .h
It is difficult to handle both pan and swipe gesture recognizer simultaneously. You will have handle the SWRevealViewController delegates for pan gesture and also swipe gesture for the current viewcontroller.
As apple suggests to differentiate the gesture you could use the following method
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Excerpt from the Apple Documentation:
This method is called when recognition of a gesture by either
gestureRecognizer or otherGestureRecognizer would block the other
gesture recognizer from recognizing its gesture. Note that returning
YES is guaranteed to allow simultaneous recognition; returning NO, on
the other hand, is not guaranteed to prevent simultaneous recognition
because the other gesture recognizer's delegate may return YES.
i have the following problem: in my iphone app i have a UIView that holds 0 to several subviews. Those subviews may overflow the parent and therefore be hidden and all of them are UIButtons. I added a UISwipeGestureRecognizer to the UIView to move the buttons around which works great. However it only works when the gesture is done on background, the UIButtons interfere with the gesture recognizer.
How i can i pass the gesture through? Btw. i still need the tap of the Buttons.
Thanks!
EDIT:
I tried to add the gesture recognizer to the UIButtons too, but it is not triggered... Although performing the swipe gesture prevents the UIButton from going to highlighted state it doesn't trigger the gesture. I added setDelaysTouchesBegan:YES as suggested in UIButton and Swipe Gesture. That's how i do it right now:
UIButton *breadcrumb = [UIButton buttonWithType:UIButtonTypeCustom];
[breadcrumb setImage:[UIImage imageNamed:#"Next"] forState:UIControlStateNormal];
[breadcrumb setTitle:title forState:UIControlStateNormal];
[breadcrumb.titleLabel setFont:[UIFont systemFontOfSize:12.0f]];
[breadcrumb sizeToFit];
[breadcrumb setTag:level];
UISwipeGestureRecognizer *gr = [[UISwipeGestureRecognizer alloc] init];
[gr setDelegate:self];
[gr setDirection:UISwipeGestureRecognizerDirectionRight];
[gr setDelaysTouchesBegan:YES];
[self.tableView addGestureRecognizer:gr];
[breadcrumb addGestureRecognizer:gr];
EDIT 2:
I have now subclassed UIButton and initilizing it now like so: [BreadcrumbButton buttonWithType:UIButtonTypeCustom]. In the initializer i added the button itsself as a listener to all touch events [self addTarget:self action:#selector(eventReceiver:) forControlEvents:UIControlEventAllTouchEvents]; to inspect whats going on.
- (void)eventReceiver:(UIButton *)btn {
NSLog(#"Reveived event: %# ---------------", btn);
for(UIGestureRecognizer *gr in ev.gestureRecognizers) {
NSLog(#"Gesture: %#", gr);
}
}
What i see is that a) the button has just one gesture recognizer added and b) that this UISwipeGestureRecognizer jumps to state Possible during swipe but does not forward to its delegate methods.
You would have to subclass the UIButton and over ride the tap delegate callback and forward this call to whatever is handling a UISwipeGestureRecognizer. Unless you add the gesture recognizer on the UIButton, it will always call it's touch handler before the view behind it. You can also explicitly tell the button to not handle it's touch events thus passing the touch event down the chain (via userInteractionEnabled), but as you've already stated you do not want this. The best way to go about this would be by creating a subclass of UIButton and handling the touch events there and/or forwarding the events using delegation. Pressing the button is a touch event, so you may just want to add a tap gesture recognizer to the button and call the IBAction from that and then have the swipegesturerecognizer forward a delegate call.