我已经制作了自定义视图类。我在视图控制器类中初始化它。我已启用用户互动,但它也无法正常工作。我在类似的问题中搜索了它,但大多数人说要启用用户互动 这是我编写的代码。
@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");
}
答案 0 :(得分:3)
你可以在这里查看几件事 profileFrame的高度和宽度都不是很小(print profileFrame)
ProfileTitle不完全透明(当视图的alpha非常接近0时,手势也不起作用)
ProfileTitle不会被任何其他视图遮挡(使用可视化调试器)
答案 1 :(得分: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");
}
添加委托并更改方法签名
答案 2 :(得分:2)
试试这些..
创建自定义视图的属性,如下所示..并将其导入到您的ProfileCreatorViewController.h文件中,如下所示..
#import "SectionTitleView.h"
@property (nonatomic , strong) SectionTitleView *previewView;
添加UIGestureRecognizerDelegate
@interface ProfileCreatorViewController ()<UIGestureRecognizerDelegate>
{
SectionTitleView *ProfileTitle;
CGRect mainFrame;
}
如果需要,请设置NumberOfTapsRequired
UITapGestureRecognizer *recog =
[[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(downButtonClicked:)];
ProfileTitle.userInteractionEnabled = YES;
[recog setNumberOfTapsRequired:1]; //No. of taps..
[ProfileTitle addGestureRecognizer:recog];
及其方法
-(void) downButtonClicked:(UITapGestureRecognizer*)gesture
{
NSLog(@"Taped");
}
我希望它有所帮助......
答案 3 :(得分:0)
您需要头文件中的UIGestureRecognizerDelegate
。
同时创建property
以保留UITapGestureRecognizer
内部:
@property (strong, nonatomic) UITapGestureRecognizer tapGesture;
您也不应该在父类中创建UITapGestureRecognizer
。 ProfileTitle 应该自行生成。因此,您最好在 SectionTitleView 中的viewDidLoad
内插入代码。
仅供参考:并且不要使用大写变量名称