UIPanGestureRecognizer in Texture/AsyncDisplayKit's ASCellNode prevents the table from scrolling when implementing `swipe to perform action` type behaviour
I am using the Texture/AsyncDisplayKit library:
https://github.com/texturegroup/texture
I am trying to implement "swipe to perform action" in a ASCellNode. The problem is that the UIPanGestureRecognizer prevents the table from scrolling.
I am able to successfully get this to work in UIKit using UITableViewCell but for some reason it's not working when using Texture's ASCellNode. I can demonstrate the issue easily with the ASDKgram example provided with this library which has both a UIKit example in one tab and Texture example in another tab:
https://github.com/TextureGroup/Texture/tree/master/examples/ASDKgram
For the UIKit example, all I had to do was:
Add <UIGestureRecognizerDelegate> to PhotoTableViewCell.h
Add UIPanGestureRecognizer *_panGestureRecognizer; in the @implementation PhotoTableViewCell
Add following to the - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier:
_panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panned:)];
_panGestureRecognizer.delegate = self;
[self.contentView addGestureRecognizer:_panGestureRecognizer];
Add following:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if (gestureRecognizer == _panGestureRecognizer) {
CGPoint v = [_panGestureRecognizer velocityInView:_panGestureRecognizer.view];
return fabs(v.x) > fabs(v.y);
}
return false;
}
-(void)panned:(UIPanGestureRecognizer *)sender{
NSLog(@"Panned!");
}
This was enough to get it to print Panned! when panning horizontally and also let the UITableView scroll when it's vertical direction.
The same does not work for the PhotoCellNode. I did the following:
Add <UIGestureRecognizerDelegate> to PhotoCellNode.h
Add UIPanGestureRecognizer *_panGestureRecognizer; in the @implementation PhotoCellNode
Add following to the PhotoCellNode.m:
- (void)didLoad {
[super didLoad];
_panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panned:)];
_panGestureRecognizer.delegate = self;
[self.view addGestureRecognizer:_panGestureRecognizer];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if (gestureRecognizer == _panGestureRecognizer) {
CGPoint v = [_panGestureRecognizer velocityInView:_panGestureRecognizer.view];
return fabs(v.x) > fabs(v.y);
}
return false;
}
-(void)panned:(UIPanGestureRecognizer *)sender{
NSLog(@"Panned!");
}
This allows Panned! to print when panning horizontally but the table does not scroll at all. Why are they working differently? How can I make the table scroll when the touches are vertical?
I was able to solve it. Though I am not sure why the behavior is different between the UITableViewCell and ASCellNode. @appleguy can you help point out why the difference?
In the PhotoCellNode.m, I returned [super gestureRecognizerShouldBegin:gestureRecognizer] in gestureRecognizerShouldBegin and I also added the shouldRecognizeSimultaneouslyWithGestureRecognizer function:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if (gestureRecognizer == _panGestureRecognizer) {
CGPoint v = [_panGestureRecognizer velocityInView:_panGestureRecognizer.view];
return fabs(v.x) > fabs(v.y);
}
return [super gestureRecognizerShouldBegin:gestureRecognizer];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return false;
}