问题
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");
}
回答1:
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)
回答2:
@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
回答3:
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..
回答4:
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
来源:https://stackoverflow.com/questions/32449919/uitapgesturerecognizer-not-working-on-custom-uiview-class