我试图使用几个手势识别器构建一个泛型类,这样我就可以调用一个方法并将两个识别器添加到视图中
这是我的类实现代码
#import "saUIHelper.h"
@implementation saUIHelper
-(void)handlePan:(UIPanGestureRecognizer *)recognizer
{
NSLog(@"pan");
}
-(void)handleLP:(UILongPressGestureRecognizer *)recognizer
{
NSLog(@"Test");
}
-(saUIHelper*)initWithView:(UIView*)view
{
self = [super init];
if(self){
self.myView = view;
self.myView.userInteractionEnabled = YES;
}
return self;
}
-(void)start
{
UIPanGestureRecognizer* panRecognizer =
[[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)];
[self.myView addGestureRecognizer:panRecognizer];
UILongPressGestureRecognizer* lpRecognizer =
[[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLP:)];
[self.myView addGestureRecognizer:lpRecognizer];
}
@end
我试着像这样使用它
UIImage *playImg = [UIImage imageNamed:@"playBtn"];
self.playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.playBtn.frame = CGRectMake(0.0f,0.0f, 60.0f, 60.0f);
self.playBtn.center = CGPointMake(viewWidth/2,(viewHeight/2)-35);
[self.playBtn setBackgroundImage:playImg forState:UIControlStateNormal];
[self.playBtn addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
[self.menu addSubview:self.playBtn];
saUIHelper* helper = [[saUIHelper alloc] initWithView:self.playBtn];
[helper start];
当我运行此代码并在我添加的按钮上执行Pan或LongPressure手势时,我的BAD_ACCESS_EXEC代码崩溃了
但是,如果不是调用[helper start];
,而是输入与start方法中实现的代码相同的代码,并将handlePan:
和handleLP:
回调添加到主类,我就不会遇到任何问题一切都很顺利
很明显,我在这里做了一些非常错误的事情,不确定它是否存在线程问题或其他问题,是否有人能给我一些指示?
答案 0 :(得分:2)
在view / viewcontroller中存储对saUIHelper* helper
的强引用,以便它不会被解除分配。您可能正在崩溃,因为目标是帮助程序,可能已被解除分配。