Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 6ae17e5

Browse files
committed
Merge pull request #403 from facebook/touch-forwarding
Forward touches to super instead of the superview
2 parents d3a8f44 + b6715b5 commit 6ae17e5

File tree

8 files changed

+139
-26
lines changed

8 files changed

+139
-26
lines changed

AsyncDisplayKit/ASCellNode.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
//@property (atomic, retain) UIColor *backgroundColor;
2121
@property (nonatomic) UITableViewCellSelectionStyle selectionStyle;
2222

23+
/*
24+
* ASCellNode must forward touch events in order for UITableView and UICollectionView tap handling to work. Overriding
25+
* these methods (e.g. for highlighting) requires the super method be called.
26+
*/
27+
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event ASDISPLAYNODE_REQUIRES_SUPER;
28+
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event ASDISPLAYNODE_REQUIRES_SUPER;
29+
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event ASDISPLAYNODE_REQUIRES_SUPER;
30+
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event ASDISPLAYNODE_REQUIRES_SUPER;
31+
2332
@end
2433

2534

AsyncDisplayKit/ASCellNode.m

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
#import "ASCellNode.h"
1010

11-
#import "ASDisplayNode+Subclasses.h"
12-
#import "ASTextNode.h"
11+
#import <AsyncDisplayKit/_ASDisplayView.h>
12+
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
13+
#import <AsyncDisplayKit/ASTextNode.h>
1314

1415

1516
#pragma mark -
@@ -28,12 +29,52 @@ - (instancetype)init
2829
return self;
2930
}
3031

32+
- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock
33+
{
34+
ASDisplayNodeAssertNotSupported();
35+
return nil;
36+
}
37+
38+
- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock
39+
{
40+
ASDisplayNodeAssertNotSupported();
41+
return nil;
42+
}
43+
3144
- (void)setLayerBacked:(BOOL)layerBacked
3245
{
3346
// ASRangeController expects ASCellNodes to be view-backed. (Layer-backing is supported on ASCellNode subnodes.)
3447
ASDisplayNodeAssert(!layerBacked, @"ASCellNode does not support layer-backing.");
3548
}
3649

50+
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
51+
{
52+
ASDisplayNodeAssertMainThread();
53+
ASDisplayNodeAssert([self.view isKindOfClass:_ASDisplayView.class], @"ASCellNode views must be of type _ASDisplayView");
54+
[(_ASDisplayView *)self.view __forwardTouchesBegan:touches withEvent:event];
55+
}
56+
57+
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
58+
{
59+
ASDisplayNodeAssertMainThread();
60+
ASDisplayNodeAssert([self.view isKindOfClass:_ASDisplayView.class], @"ASCellNode views must be of type _ASDisplayView");
61+
[(_ASDisplayView *)self.view __forwardTouchesMoved:touches withEvent:event];
62+
}
63+
64+
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
65+
{
66+
ASDisplayNodeAssertMainThread();
67+
ASDisplayNodeAssert([self.view isKindOfClass:_ASDisplayView.class], @"ASCellNode views must be of type _ASDisplayView");
68+
[(_ASDisplayView *)self.view __forwardTouchesEnded:touches withEvent:event];
69+
}
70+
71+
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
72+
{
73+
ASDisplayNodeAssertMainThread();
74+
ASDisplayNodeAssert([self.view isKindOfClass:_ASDisplayView.class], @"ASCellNode views must be of type _ASDisplayView");
75+
[(_ASDisplayView *)self.view __forwardTouchesCancelled:touches withEvent:event];
76+
}
77+
3778
@end
3879

3980

AsyncDisplayKit/ASDisplayNode.mm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,22 +1424,22 @@ - (void)recursivelySetNeedsDisplayAtScale:(CGFloat)contentsScale
14241424

14251425
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
14261426
{
1427-
// subclass hook
1427+
// subclass hook
14281428
}
14291429

14301430
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
14311431
{
1432-
// subclass hook
1432+
// subclass hook
14331433
}
14341434

14351435
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
14361436
{
1437-
// subclass hook
1437+
// subclass hook
14381438
}
14391439

14401440
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
14411441
{
1442-
// subclass hook
1442+
// subclass hook
14431443
}
14441444

14451445
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

AsyncDisplayKit/ASEditableTextNode.mm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ - (instancetype)init
8585
return self;
8686
}
8787

88+
- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock
89+
{
90+
ASDisplayNodeAssertNotSupported();
91+
return nil;
92+
}
93+
94+
- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock
95+
{
96+
ASDisplayNodeAssertNotSupported();
97+
return nil;
98+
}
99+
88100
- (void)dealloc
89101
{
90102
_textKitComponents.textView.delegate = nil;

AsyncDisplayKit/ASImageNode.mm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ - (id)init
9494
return self;
9595
}
9696

97+
- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock
98+
{
99+
ASDisplayNodeAssertNotSupported();
100+
return nil;
101+
}
102+
103+
- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock
104+
{
105+
ASDisplayNodeAssertNotSupported();
106+
return nil;
107+
}
108+
97109
- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
98110
{
99111
ASDN::MutexLocker l(_imageLock);

AsyncDisplayKit/ASTextNode.mm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ - (instancetype)init
146146
return self;
147147
}
148148

149+
- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock
150+
{
151+
ASDisplayNodeAssertNotSupported();
152+
return nil;
153+
}
154+
155+
- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock
156+
{
157+
ASDisplayNodeAssertNotSupported();
158+
return nil;
159+
}
160+
149161
- (void)dealloc
150162
{
151163
if (_shadowColor != NULL) {

AsyncDisplayKit/Details/_ASDisplayView.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@
1414

1515
@interface _ASDisplayView : UIView
1616

17+
// These methods expose a way for ASDisplayNode touch events to let the view call super touch events
18+
// Some UIKit mechanisms, like UITableView and UICollectionView selection handling, require this to work
19+
- (void)__forwardTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
20+
- (void)__forwardTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
21+
- (void)__forwardTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
22+
- (void)__forwardTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
23+
1724
@end

AsyncDisplayKit/Details/_ASDisplayView.mm

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -151,38 +151,58 @@ - (void)setContentMode:(UIViewContentMode)contentMode
151151
#pragma mark - Event Handling + UIResponder Overrides
152152
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
153153
{
154-
if (_node.methodOverrides & ASDisplayNodeMethodOverrideTouchesBegan) {
155-
[_node touchesBegan:touches withEvent:event];
156-
} else {
157-
[super touchesBegan:touches withEvent:event];
158-
}
154+
if (_node.methodOverrides & ASDisplayNodeMethodOverrideTouchesBegan) {
155+
[_node touchesBegan:touches withEvent:event];
156+
} else {
157+
[super touchesBegan:touches withEvent:event];
158+
}
159159
}
160160

161161
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
162162
{
163-
if (_node.methodOverrides & ASDisplayNodeMethodOverrideTouchesMoved) {
164-
[_node touchesMoved:touches withEvent:event];
165-
} else {
166-
[super touchesMoved:touches withEvent:event];
167-
}
163+
if (_node.methodOverrides & ASDisplayNodeMethodOverrideTouchesMoved) {
164+
[_node touchesMoved:touches withEvent:event];
165+
} else {
166+
[super touchesMoved:touches withEvent:event];
167+
}
168168
}
169169

170170
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
171171
{
172-
if (_node.methodOverrides & ASDisplayNodeMethodOverrideTouchesEnded) {
173-
[_node touchesEnded:touches withEvent:event];
174-
} else {
175-
[super touchesEnded:touches withEvent:event];
176-
}
172+
if (_node.methodOverrides & ASDisplayNodeMethodOverrideTouchesEnded) {
173+
[_node touchesEnded:touches withEvent:event];
174+
} else {
175+
[super touchesEnded:touches withEvent:event];
176+
}
177177
}
178178

179179
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
180180
{
181-
if (_node.methodOverrides & ASDisplayNodeMethodOverrideTouchesCancelled) {
182-
[_node touchesCancelled:touches withEvent:event];
183-
} else {
184-
[super touchesCancelled:touches withEvent:event];
185-
}
181+
if (_node.methodOverrides & ASDisplayNodeMethodOverrideTouchesCancelled) {
182+
[_node touchesCancelled:touches withEvent:event];
183+
} else {
184+
[super touchesCancelled:touches withEvent:event];
185+
}
186+
}
187+
188+
- (void)__forwardTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
189+
{
190+
[super touchesBegan:touches withEvent:event];
191+
}
192+
193+
- (void)__forwardTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
194+
{
195+
[super touchesMoved:touches withEvent:event];
196+
}
197+
198+
- (void)__forwardTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
199+
{
200+
[super touchesEnded:touches withEvent:event];
201+
}
202+
203+
- (void)__forwardTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
204+
{
205+
[super touchesCancelled:touches withEvent:event];
186206
}
187207

188208
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

0 commit comments

Comments
 (0)