Skip to content

Commit 6298f32

Browse files
committed
new APIs for responding to different cell tap events. issue jessesquires#374. update demo.
1 parent 2f9a222 commit 6298f32

File tree

5 files changed

+94
-4
lines changed

5 files changed

+94
-4
lines changed

JSQMessagesDemo/JSQDemoViewController.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ - (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collection
405405

406406
#pragma mark - JSQMessages collection view flow layout delegate
407407

408+
#pragma mark - Adjusting cell label heights
409+
408410
- (CGFloat)collectionView:(JSQMessagesCollectionView *)collectionView
409411
layout:(JSQMessagesCollectionViewFlowLayout *)collectionViewLayout heightForCellTopLabelAtIndexPath:(NSIndexPath *)indexPath
410412
{
@@ -452,10 +454,27 @@ - (CGFloat)collectionView:(JSQMessagesCollectionView *)collectionView
452454
return 0.0f;
453455
}
454456

457+
#pragma mark - Responding to collection view tap events
458+
455459
- (void)collectionView:(JSQMessagesCollectionView *)collectionView
456460
header:(JSQMessagesLoadEarlierHeaderView *)headerView didTapLoadEarlierMessagesButton:(UIButton *)sender
457461
{
458462
NSLog(@"Load earlier messages!");
459463
}
460464

465+
- (void)collectionView:(JSQMessagesCollectionView *)collectionView didTapAvatarImageView:(UIImageView *)avatarImageView atIndexPath:(NSIndexPath *)indexPath
466+
{
467+
NSLog(@"Tapped avatar!");
468+
}
469+
470+
- (void)collectionView:(JSQMessagesCollectionView *)collectionView didTapMessageBubbleAtIndexPath:(NSIndexPath *)indexPath
471+
{
472+
NSLog(@"Tapped message bubble!");
473+
}
474+
475+
- (void)collectionView:(JSQMessagesCollectionView *)collectionView didTapCellAtIndexPath:(NSIndexPath *)indexPath touchLocation:(CGPoint)touchLocation
476+
{
477+
NSLog(@"Tapped cell at %@!", NSStringFromCGPoint(touchLocation));
478+
}
479+
461480
@end

JSQMessagesViewController/Controllers/JSQMessagesViewController.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,19 @@ - (void)messagesCollectionViewCellDidTapAvatar:(JSQMessagesCollectionViewCell *)
516516
atIndexPath:[self.collectionView indexPathForCell:cell]];
517517
}
518518

519+
- (void)messagesCollectionViewCellDidTapMessageBubble:(JSQMessagesCollectionViewCell *)cell
520+
{
521+
[self.collectionView.delegate collectionView:self.collectionView
522+
didTapMessageBubbleAtIndexPath:[self.collectionView indexPathForCell:cell]];
523+
}
524+
525+
- (void)messagesCollectionViewCellDidTapCell:(JSQMessagesCollectionViewCell *)cell atPosition:(CGPoint)position
526+
{
527+
[self.collectionView.delegate collectionView:self.collectionView
528+
didTapCellAtIndexPath:[self.collectionView indexPathForCell:cell]
529+
touchLocation:position];
530+
}
531+
519532
#pragma mark - Input toolbar delegate
520533

521534
- (void)messagesInputToolbar:(JSQMessagesInputToolbar *)toolbar didPressLeftBarButton:(UIButton *)sender

JSQMessagesViewController/Views/JSQMessagesCollectionView.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,32 @@
208208
didTapAvatarImageView:(UIImageView *)avatarImageView
209209
atIndexPath:(NSIndexPath *)indexPath;
210210

211+
/**
212+
* Notifies the delegate that the message bubble at the specified indexPath did receive a tap event.
213+
*
214+
* @param collectionView The collection view object that is notifying you of the tap event.
215+
* @param indexPath The index path of the item for which the message bubble was tapped.
216+
*/
217+
- (void)collectionView:(JSQMessagesCollectionView *)collectionView didTapMessageBubbleAtIndexPath:(NSIndexPath *)indexPath;
218+
219+
/**
220+
* Notifies the delegate that the cell at the specified indexPath did receive a tap event at the specified touchLocation.
221+
*
222+
* @param collectionView The collection view object that is notifying you of the tap event.
223+
* @param indexPath The index path of the item for which the message bubble was tapped.
224+
* @param touchLocation The location of the touch event in the cell's coordinate system.
225+
*
226+
* @warning This method is *only* called if position is *not* within the bounds of the cell's
227+
* avatar image view or message bubble image view. In other words, this method is *not* called when the cell's
228+
* avatar or message bubble are tapped. There are separate delegate methods for these two cases.
229+
*
230+
* @see `collectionView:didTapAvatarImageView:atIndexPath:`
231+
* @see `collectionView:didTapMessageBubbleAtIndexPath:atIndexPath:`
232+
*/
233+
- (void)collectionView:(JSQMessagesCollectionView *)collectionView
234+
didTapCellAtIndexPath:(NSIndexPath *)indexPath
235+
touchLocation:(CGPoint)touchLocation;
236+
211237
/**
212238
* Notifies the delegate that the collection view's header did receive a tap event.
213239
*

JSQMessagesViewController/Views/JSQMessagesCollectionViewCell.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,34 @@
3131
@required
3232

3333
/**
34-
* Tells the delegate that the avatarImageView of a cell has been tapped.
34+
* Tells the delegate that the avatarImageView of the cell has been tapped.
3535
*
36-
* @param cell The cell that received the tap.
36+
* @param cell The cell that received the tap touch event.
3737
*/
3838
- (void)messagesCollectionViewCellDidTapAvatar:(JSQMessagesCollectionViewCell *)cell;
3939

40+
/**
41+
* Tells the delegate that the message bubble of the cell has been tapped.
42+
*
43+
* @param cell The cell that received the tap touch event.
44+
*/
45+
- (void)messagesCollectionViewCellDidTapMessageBubble:(JSQMessagesCollectionViewCell *)cell;
46+
47+
/**
48+
* Tells the delegate that the cell has been tapped at the point specified by position.
49+
*
50+
* @param cell The cell that received the tap touch event.
51+
* @param position The location of the received touch in the cell's coordinate system.
52+
*
53+
* @discussion This method is *only* called if position is *not* within the bounds of the cell's
54+
* avatar image view or message bubble image view. In other words, this method is *not* called when the cell's
55+
* avatar or message bubble are tapped.
56+
*
57+
* @see `messagesCollectionViewCellDidTapAvatar:`
58+
* @see `messagesCollectionViewCellDidTapMessageBubble:`
59+
*/
60+
- (void)messagesCollectionViewCellDidTapCell:(JSQMessagesCollectionViewCell *)cell atPosition:(CGPoint)position;
61+
4062
@end
4163

4264

JSQMessagesViewController/Views/JSQMessagesCollectionViewCell.m

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ - (void)awakeFromNib
131131
self.longPressGestureRecognizer = longPress;
132132

133133
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jsq_handleTapGesture:)];
134-
[self.avatarContainerView addGestureRecognizer:tap];
134+
[self addGestureRecognizer:tap];
135135
self.tapGestureRecognizer = tap;
136136
}
137137

@@ -363,7 +363,17 @@ - (void)jsq_handleLongPressGesture:(UILongPressGestureRecognizer *)longPress
363363

364364
- (void)jsq_handleTapGesture:(UITapGestureRecognizer *)tap
365365
{
366-
[self.delegate messagesCollectionViewCellDidTapAvatar:self];
366+
CGPoint touchPt = [tap locationInView:self];
367+
368+
if (CGRectContainsPoint(self.avatarContainerView.frame, touchPt)) {
369+
[self.delegate messagesCollectionViewCellDidTapAvatar:self];
370+
}
371+
else if (CGRectContainsPoint(self.messageBubbleContainerView.frame, touchPt)) {
372+
[self.delegate messagesCollectionViewCellDidTapMessageBubble:self];
373+
}
374+
else {
375+
[self.delegate messagesCollectionViewCellDidTapCell:self atPosition:touchPt];
376+
}
367377
}
368378

369379
#pragma mark - Notifications

0 commit comments

Comments
 (0)