Skip to content

Instantly share code, notes, and snippets.

@cemolcay
Created February 25, 2015 08:02
Show Gist options
  • Save cemolcay/c4fe49ea2c34ce8ddab6 to your computer and use it in GitHub Desktop.
Save cemolcay/c4fe49ea2c34ce8ddab6 to your computer and use it in GitHub Desktop.
uiview with tap gesture action
#import <UIKit/UIKit.h>
typedef void(^tapAction)(UITapGestureRecognizer *tap);
@interface BlockTapView : UIView
@property (copy) tapAction blockTapAction;
- (instancetype)initWithFrame:(CGRect)frame andAction:(tapAction)action;
@end
@implementation BlockTapView
- (instancetype)initWithFrame:(CGRect)frame andAction:(tapAction)action {
if ((self = [super initWithFrame:frame])) {
self.blockTapAction = action;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap:)];
[self addGestureRecognizer:tap];
}
return self;
}
- (void)didTap:(UITapGestureRecognizer *)tap {
if (self.blockTapAction) {
self.blockTapAction (tap);
}
}
@end
@cemolcay
Copy link
Author

in DroppySection.m file, the init method registers a tap gesture to section view called sectionTapped:

- (instancetype)initWithHeaderView:(UIView *)headerView index:(NSInteger)index andPadding:(CGFloat)padding {
    if ((self = [super init])) {
        self.headerView = headerView;
        self.index = index;
        self.padding = padding;

        self.views = [[NSMutableArray alloc] init];
        self.expanded = NO;
        self.expanding = NO;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionTapped:)];
        [headerView addGestureRecognizer:tap];
        [headerView setUserInteractionEnabled:YES];
    }
    return self;
}


- (void)sectionTapped:(UITapGestureRecognizer *)tap {
    self.expanding = YES;
    self.expanded = !self.expanded;

    if (self.isExpanded) {
        [self expand];
    } else {
        [self collapse];
    }
}

i think button cant trigger because the tap gesutre catches the event first..
you can edit the sectionTapped: method for section tap event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment