How to unblock pan and pinch events in a UIScrollViewDelegate? - ios

I have a UIView which has a UIScrollView subview and is a UIScrollViewDelegate. Since I've implemented viewForZoomingInScrollView I stopped getting pan and pinch events for the UIView.
How can I get these events back?

Try adding a delegate to your gesture recognizer that implements
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}

try:
[yourviewname setUserInteractionEnabled:YES];
Edit: I just re-read your question. Are you saying that you can't pan and/or pinch? Or are you saying that you aren't being notified when you pan and/or pinch?

Related

Enabling touch event on Objects In a UIScrollview

I've a scrollview that has a UIView on it and on those Views, there's a UIImageView on it, three UILabel on it, I want to enable user Interaction on it, but wouldn't work. I've enabled setUserInteraction for both the UIView, UIScrollView, UILabels, UIImageView none is Responding to click actions at all. The layout look like the Image Below....
implement
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
so the gestures of the uiscroll view won't conflict with the subview's recognisers
basically what happens is that the UIScrollView won't pass the events to its subviews because the default of the above method is to return NO
This sounds too easy to be the problem, but I've made the following mistake: Everything is set just right, but I did not drag out one of the SentEvents like TouchUpInside to an action in the implementation.
check this way,
1> first check whether your view controller respond to UIGestureRecognizerDelegate this way
#interface RootViewController : UIViewController<UIGestureRecognizerDelegate>
2> then in design check you map scrollview property and delegate properly
3> apply this code in viewDidload
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapditected)];
tapGesture.delegate = self;
// prevents the scroll view from swallowing up the touch event of child buttons
tapGesture.cancelsTouchesInView = NO;
[self.scroller addGestureRecognizer:tapGesture];
4> check you apply this code
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
5> if all goes wll then this method should detected
-(void)tapditected{
}

Touch Events on UIView on UITableView

I'm trying to catch the touchesBegan and the touchesMoved events on an UIView.
I have a UITableView; each cell contains a UIView with several controls. One of these controls is a subclass of a UIView with code in the touchesBegan and touchesMoved events.
If this control is not on the tableviewcell, then it works fine. But in this situation, all touches have effect to the tableview, but not to the UIView.
How to ignore the TouchEvents and redirect them to the UIView?
Try setting delaysContentTouches property of the tableview(scrollview) to YES.
Try this one..
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isDescendantOfView:yourview])
{
return NO;
}
return YES;
}

Getting all touch events of an app

I've been looking for a way to intercept all touch events of an app.
I saw that I can add a gesture recognizer to the main window and get all the touches by using it's delegate method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
NSLog(#"%#",touch);
return NO;
}
This way I don't harm all of the other touch events the app has.
The problem is that I cannot get swipe events and such this way. Moreover, I cannot override UIWindow sendEvents because my app is an outside framework.
I also do not want to add a transparent UIView on top.
Is there any other way to get swipes and other gesturs?
To intercept all touch events of an app, use a UIWindow subclass and override sendEvent:.... Every touch event passes through this bottleneck method. Be warned that you are now operating at a very low level and can easily break everything.
I managed to get this to work by using a custom gesture recognizer attached to the keyWindow and with using the method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Which also gives me all the touches and doesn't disturb any other recognizers.
If anyone know why I shouldn't be doing that, I'd be happy to hear about it.

UIScrollView overrides my subview's pan gesture recognizers

If I have a scrollView with a subview and the subview has a pan gesture recognizer, the scrollView's pan gesture override's the subview's pan. What I want is the opposite, I think, so that is I drag a subview it will pan within the scroll view, yet if I touch another area the scroll view will pan as normal. Is there an easy way to set that up?
Here's what works for me:
UIPanGestureRecognizer *subviewPanRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(panSubview:)];
[subview addGestureRecognizer:subviewPanRecognizer];
// play nice with subview's pan gesture
[scrollView.panGestureRecognizer
requireGestureRecognizerToFail:subviewPanRecognizer];
Set canCancelContentTouches property of UIScrollView to false if you don't want to scroll on touching subviews.
Original answer
Overwrite these two delegate below,
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
This will allow you to recognize both gestures, the default return is NO, so we need to overwrite it and return YES.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
if ([otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
return NO;
}else{
return YES;
}
}
return YES;
}
In this delegate you can do anything as you wish, as it's name the gestureRecoginzer will be required to fail by the otherGestureRecognizer, all you need to do is to judge what kind of these two gestures and return YES or NO.

Receiving gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer when gestureRecognizerShouldBegin: returned NO

I have a view that has a UIPanGestureRecognizer. That same view also contains a UIScrollView.
When the user pans over the ScrollView, I return NO in gestureRecognizerShouldBegin:, to make sure that the ScrollView's gesture recognizer takes over the gesture.
However, I still receive the gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: message. It is easy to fix, I simply need to return NO in gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:, but I was wondering if it is normal to receive this message after returning NO to gestureRecognizerShouldBegin:.
Code
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
NSLog(#"gestureRecognizerShouldBegin:0x%x - return NO", (int)gestureRecognizer);
return NO;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
NSLog(#"gestureRecognizer:0x%x shouldRecognizeSimultaneouslyWithGestureRecognizer:0x%x", (int)gestureRecognizer, (int)otherGestureRecognizer);
return YES;
}
Output
2013-03-04 11:30:14.876 XXXX[99271:19d03] gestureRecognizerShouldBegin:0x9d6c380 - return NO
2013-03-04 11:30:14.876 XXXX[99271:19d03] gestureRecognizer:0x9d6c380 shouldRecognizeSimultaneouslyWithGestureRecognizer:0xbb75240
I think this behaviour is ok.
From the following reference of gestureRecognizerShouldBegin: method from official doc Discussion:
This method is called when a gesture recognizer attempts to transition out of the UIGestureRecognizerStatePossible state. Returning NO causes the gesture recognizer to transition to the UIGestureRecognizerStateFailed state.
This clearly indicates that it will not prevent the gesture recognizer but it will make the transition to UIGestureRecognizerStateFailed state of respective gesture recognizer. So other delegate methods will be called as normal.

Resources