我正在尝试创建一个类似于按钮的UIView类。我想完全以编程方式做到这一点。我能够显示该按钮,但单击不正确。
在我的类实现文件中,我具有初始化方法以及以下内容:
- (void)handlePress:(UILongPressGestureRecognizer *)sender{
if(sender.state == UIGestureRecognizerStateBegan){
NSLog(@"in handlePress");
}
}
在我的视图控制器实现文件中,我有:
- (void)viewDidLoad
{
[super viewDidLoad];
self.button = [[buttonclass alloc] initButton];
[self.view addSubview: self.button];
UILongPressGestureRecognizer *singlePress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlePress:)];
[self.button.buttonView addGestureRecognizer:singlePress];
}
(button是类buttonclass的实例变量,而buttonView只是button的子视图)
我很确定问题出在问题之内:
UILongPressGestureRecognizer *singlePress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlePress:)];
[self.button.buttonView addGestureRecognizer:singlePress];
我什至不确定这些行是否应该放在按钮初始化函数中或视图控制器实现文件中。我应该添加以下行吗?
singlePress.delegate = self;
我尝试添加该行,但我不知道将其放置在何处,并且出现错误,并指出delegate
和self
不是相同的类型。无论如何,当我尝试将GestureRecognizer连接到动作或将GestureRecognizer连接到UIView按钮时,都会出问题。
任何帮助将不胜感激!谢谢,祝你有美好的一天。
您应该将手势添加到按钮而不是按钮的子视图上。
UILongPressGestureRecognizer *singlePress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlePress:)];
[self.button addGestureRecognizer:singlePress];
无需添加以下行:
singlePress.delegate = self;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句