0

I have been trying to do this simple thing : adding an action to a simple custom view. I have looked over the internet and found two "easy" solution :

  • UITapGestureRecognizer
  • UIButton

I want to do this programmatically and I just need to handle the tap.

Here is my code so far, I've tried both solutions separately and together and it doesn't work !

.m

#import "AreaView.h"
@implementation AreaView
#define GREY 27.0/255.0
#define PINK_R 252.0/255.0
#define PINK_G 47.0/255.0
#define PINK_B 99.0/255.0

- (id) initWithFrame:(CGRect)frame imageName:(NSString *)imageName areaName:(NSString *)areaName minimumSpending:(int)minimumSpending andCapacity:(int)capacity
{
    self = [self initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:GREY green:GREY blue:GREY alpha:1];
        self.userInteractionEnabled=YES;
        //Init variables
        _areaName=areaName;
        _capacity=capacity;
        _minimumSpending=minimumSpending;

        //Image view
        _logoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 4, 66, 50)];
        //_logoImageView.image = [UIImage imageNamed:imageName];
        _logoImageView.backgroundColor = [UIColor grayColor];

        //Label
        _areaNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 54, 76, 18)];
        _areaNameLabel.textAlignment = NSTextAlignmentCenter;
        _areaNameLabel.textColor = [UIColor whiteColor];
        _areaNameLabel.font = [UIFont systemFontOfSize:12.0];
        _areaNameLabel.text = areaName;

        //button
        _button = [[UIButton alloc]initWithFrame:self.bounds];
        _button.userInteractionEnabled=YES;
        _button.backgroundColor=[UIColor yellowColor];
        [_button addTarget:self action:@selector(handleTap:) forControlEvents:UIControlEventTouchUpInside];

        //tap gesture racognizer
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
        [tapRecognizer setNumberOfTapsRequired:1];
        [tapRecognizer setDelegate:self];
        [self addGestureRecognizer:tapRecognizer];

        [self addSubview:_logoImageView];
        [self addSubview:_areaNameLabel];
        [self addSubview:_button];
    }
    return self;
}


-(void)handleTap:(UIButton *)button
{
    NSLog(@"tapped!");
}

-(void)tapped:(UITapGestureRecognizer *)recognizer
{
    NSLog(@"tapped!");
}
@end

.h

#import <UIKit/UIKit.h>

@interface AreaView : UIView <UIGestureRecognizerDelegate>

@property (nonatomic) UIImageView *logoImageView;
@property (nonatomic) UILabel *areaNameLabel;
@property (nonatomic) NSString *areaName;
@property (nonatomic) int minimumSpending;
@property (nonatomic) int capacity;
- (id) initWithFrame:(CGRect)frame imageName:(NSString *)imageName areaName:(NSString *)areaName minimumSpending:(int)minimumSpending andCapacity:(int)capacity;
@property (nonatomic, strong) UIButton *button;
@end

Thanks for your help!

EDIT

The problem is that both handleTap and tapped are never fired even if I comment the button solution or the tap gesture one to test them separately. For the button implementation, I can see it on my interface but clicking on it does nothing.

My UIView is then added programmatically several times (for several views) in a UIScrollview.

EDIT 2

The problem is more complicate than that. The custom view is inside a scrollview which is inside another different custom view whose main function is to rewrite hittest, so that touches on this view are held by the scrollview. (Here is the purpose of all that).

It seems that as long as hittest is involved, it doesn't work.

Ambroise Collon
  • 3,839
  • 3
  • 18
  • 37

2 Answers2

0

Implement this delegate method, which will help you.

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
           id touchedView = gestureRecognizer.view;
           if ([touchedView isKindOfClass:[UIButton class]])
           {
                return NO; //It won't invoke gesture method, But it'll fire button method.
            }
           return YES;
    }
Mani
  • 17,549
  • 13
  • 79
  • 100
  • It's not exactly what I was looking for. I don't want multiple solutions. I have tested two but I need only one to work : Button or Gesture and it doesn't. Thanks – Ambroise Collon Mar 06 '14 at 14:01
0

I'm not sure why your UIButton and UITapGestureRecognizer selectors aren't firing. However another option to handle taps on a view is to simply override the touchesEnded:withEvent method:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    //handle a tap
}

That way, you won't have to create a UIButton or a UITapGestureRecognizer object at all.

hgwhittle
  • 9,316
  • 6
  • 48
  • 60