Created
February 25, 2015 08:02
-
-
Save cemolcay/c4fe49ea2c34ce8ddab6 to your computer and use it in GitHub Desktop.
uiview with tap gesture action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
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
Thank Cem. How do I call this? I'm trying to add a tap gesture to the sub-buttons - the main buttons work fine. The expanded buttons are where I'm scratching my head. Currently, I have this:
(UIButton *)sectionViewWithTitle:(NSString *)title {
//NSLog(@"%s", FUNCTION);
UIButton *btn = [[UIButton alloc] init];
CGRect frame = CGRectMake(10, 40, [self.view w] - 40, 60);
btn.frame = frame;
[btn setTitle:title forState:UIControlStateNormal];
[btn.titleLabel setTextAlignment:NSTextAlignmentCenter];
[btn.titleLabel setTextColor:[UIColor blackColor]];
[btn.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:25]];
[btn setBackgroundColor:[self randomColor]];
[btn addTarget:self action:@selector(btnPress:) forControlEvents:UIControlEventTouchUpInside];
return btn;
}
(void) btnPress: (id) sender {
NSLog(@"button pressed");
}
I'm just trying to get btnPress to fire - and also to identify which button it was that fired. Advice welcome!!