简体   繁体   中英

UITapGestureRecognizer not working on custom UIView class

I've made a custom view class. I'm initializing it in my view controller class. I've enabled user interaction then also it's not working. I've searched it in similar questions but most of them say to enable user interaction.
Here's the code I've written.

@interface ProfileCreatorViewController (){

SectionTitleView *ProfileTitle;
CGRect mainFrame;

}

@end

@implementation ProfileCreatorViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    mainFrame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + NAVBAR_HEIGHT, self.view.frame.size.width, self.view.frame.size.height);

    CGRect profileFrame = CGRectMake(mainFrame.origin.x + 5, mainFrame.origin.y, mainFrame.size.width - 20, 50);
    ProfileTitle = [[SectionTitleView alloc]initWithFrame:profileFrame withTitle:@"Profile" withUnderLineColor:[UIColor blackColor] withDownButton:[UIImage imageNamed:@"rightArrow"]];
    [self.view addSubview:ProfileTitle];

    UITapGestureRecognizer *recog = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(downButtonClicked)];
    [ProfileTitle addGestureRecognizer:recog];
    ProfileTitle.userInteractionEnabled = YES;

}


-(void) downButtonClicked{

    NSLog(@"clicked");
}

You can check couple of things here profileFrame's height and width are not very small (print profileFrame)

ProfileTitle is not complete transparent (Gesture will also not work when view's alpha is very close to 0)

ProfileTitle is not obscured by any other view (use visual debugger for that)

@interface ProfileCreatorViewController ()<UIGestureRecognizerDelegate>{

SectionTitleView *ProfileTitle;
CGRect mainFrame;

}

@end

@implementation ProfileCreatorViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    mainFrame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + NAVBAR_HEIGHT, self.view.frame.size.width, self.view.frame.size.height);

    CGRect profileFrame = CGRectMake(mainFrame.origin.x + 5, mainFrame.origin.y, mainFrame.size.width - 20, 0);
    ProfileTitle = [[SectionTitleView alloc]initWithFrame:profileFrame withTitle:@"Profile" withUnderLineColor:[UIColor blackColor] withDownButton:[UIImage imageNamed:@"rightArrow"]];
    [self.view addSubview:ProfileTitle];

    UITapGestureRecognizer *recog = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(downButtonClicked:)];
    recog.delegate=self;
    [ProfileTitle addGestureRecognizer:recog];
    ProfileTitle.userInteractionEnabled = YES;

}


-(void) downButtonClicked:(UITapGestureRecognizer *)sender{

    NSLog(@"clicked");
}

Add a delegate and change your method signature

Try these..

create property of your custom view like these..and also import it in your ProfileCreatorViewController.h file like these..

#import "SectionTitleView.h"

@property (nonatomic , strong) SectionTitleView *previewView;

Add UIGestureRecognizerDelegate

@interface ProfileCreatorViewController ()<UIGestureRecognizerDelegate>
{
   SectionTitleView *ProfileTitle;
   CGRect mainFrame;
}

set NumberOfTapsRequired if you want

UITapGestureRecognizer *recog = 
  [[UITapGestureRecognizer alloc]initWithTarget:self 
                                         action:@selector(downButtonClicked:)];     
ProfileTitle.userInteractionEnabled = YES;
[recog setNumberOfTapsRequired:1];  //No. of taps..
[ProfileTitle addGestureRecognizer:recog];

and also its method

-(void) downButtonClicked:(UITapGestureRecognizer*)gesture
{
    NSLog(@"Taped");
}

i hope it helps..

You need the UIGestureRecognizerDelegate inside the header file. Also create a property to hold the UITapGestureRecognizer inside:

@property (strong, nonatomic) UITapGestureRecognizer tapGesture;

You also shouldn't create the UITapGestureRecognizer inside the parent class. The ProfileTitle should generate this by itself. So you'd better insert the code inside viewDidLoad inside SectionTitleView .

FYI: And don't use capitalized variable names

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM