Skip to content

Commit

Permalink
Add onTapBlankArea and onTapAllArea callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
燕泽堃(燕疏) committed Dec 11, 2018
1 parent 0be50f9 commit daec0bd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
5 changes: 5 additions & 0 deletions TTGTagCollectionView/Classes/TTGTagCollectionView.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ typedef NS_ENUM(NSInteger, TTGTagCollectionAlignment) {
@property (nonatomic, assign) BOOL showsHorizontalScrollIndicator;
@property (nonatomic, assign) BOOL showsVerticalScrollIndicator;

// Tap blank area callback
@property (nonatomic, copy) void (^onTapBlankArea)(CGPoint location);
// Tap all area callback
@property (nonatomic, copy) void (^onTapAllArea)(CGPoint location);

/**
* Reload all tag cells
*/
Expand Down
23 changes: 21 additions & 2 deletions TTGTagCollectionView/Classes/TTGTagCollectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,27 @@ - (NSInteger)indexOfTagAt:(CGPoint)point {
#pragma mark - Gesture

- (void)onTapGesture:(UITapGestureRecognizer *)tapGesture {
CGPoint tapPointInCollectionView = [tapGesture locationInView:self];

if (![self.dataSource respondsToSelector:@selector(numberOfTagsInTagCollectionView:)] ||
![self.dataSource respondsToSelector:@selector(tagCollectionView:tagViewForIndex:)] ||
![self.delegate respondsToSelector:@selector(tagCollectionView:didSelectTag:atIndex:)]) {
if (_onTapBlankArea) {
_onTapBlankArea(tapPointInCollectionView);
}
if (_onTapAllArea) {
_onTapAllArea(tapPointInCollectionView);
}
return;
}

CGPoint tapPoint = [tapGesture locationInView:_containerView];
CGPoint tapPointInScrollView = [tapGesture locationInView:_containerView];
BOOL hasLocatedToTag = NO;

for (NSUInteger i = 0; i < [self.dataSource numberOfTagsInTagCollectionView:self]; i++) {
UIView *tagView = [self.dataSource tagCollectionView:self tagViewForIndex:i];
if (CGRectContainsPoint(tagView.frame, tapPoint) && !tagView.isHidden) {
if (CGRectContainsPoint(tagView.frame, tapPointInScrollView) && !tagView.isHidden) {
hasLocatedToTag = YES;
if ([self.delegate respondsToSelector:@selector(tagCollectionView:shouldSelectTag:atIndex:)]) {
if ([self.delegate tagCollectionView:self shouldSelectTag:tagView atIndex:i]) {
[self.delegate tagCollectionView:self didSelectTag:tagView atIndex:i];
Expand All @@ -119,6 +129,15 @@ - (void)onTapGesture:(UITapGestureRecognizer *)tapGesture {
}
}
}

if (!hasLocatedToTag) {
if (_onTapBlankArea) {
_onTapBlankArea(tapPointInCollectionView);
}
}
if (_onTapAllArea) {
_onTapAllArea(tapPointInCollectionView);
}
}

#pragma mark - Override
Expand Down
6 changes: 6 additions & 0 deletions TTGTagCollectionView/Classes/TTGTextTagCollectionView.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

// Extra data. You can use this to bind any object you want to each tag.
@property (nonatomic, strong) NSObject *extraData;

@end

/// TTGTextTagCollectionView
Expand Down Expand Up @@ -140,6 +141,11 @@
@property (nonatomic, assign) BOOL showsHorizontalScrollIndicator;
@property (nonatomic, assign) BOOL showsVerticalScrollIndicator;

// Tap blank area callback
@property (nonatomic, copy) void (^onTapBlankArea)(CGPoint location);
// Tap all area callback
@property (nonatomic, copy) void (^onTapAllArea)(CGPoint location);

// Reload
- (void)reload;

Expand Down
16 changes: 16 additions & 0 deletions TTGTagCollectionView/Classes/TTGTextTagCollectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,22 @@ - (BOOL)showsVerticalScrollIndicator {
return _tagCollectionView.showsVerticalScrollIndicator;
}

- (void)setOnTapBlankArea:(void (^)(CGPoint location))onTapBlankArea {
_tagCollectionView.onTapBlankArea = onTapBlankArea;
}

- (void (^)(CGPoint location))onTapBlankArea {
return _tagCollectionView.onTapBlankArea;
}

- (void)setOnTapAllArea:(void (^)(CGPoint location))onTapAllArea {
_tagCollectionView.onTapAllArea = onTapAllArea;
}

- (void (^)(CGPoint location))onTapAllArea {
return _tagCollectionView.onTapAllArea;
}

#pragma mark - Private methods

- (void)updateAllLabelStyleAndFrame {
Expand Down

0 comments on commit daec0bd

Please sign in to comment.