我写了一个简单的自定义手势识别器:
@interface myRecognizer : UIGestureRecognizer {
NSString * _name;
CGPoint _startPoint;
}
- (void)resetMe;
- (void)reset;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
@end
我发起了这样的话:
myRecognizer *recognizer = [[myRecognizer alloc] initWithTarget:self action:@selector(didRecognize_myRecognizer:)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];
到目前为止一切顺利。我在这里采取行动:
-(void)didRecognize_myRecognizer:(UIGestureRecognizer*)theRecognizer
{
//do stuff here
}
好的,到目前为止这个工作正常,一切都很好。但是,这是我的问题,我怎样才能访问我在自定义手势中创建的_name属性//在这里做什么?基本上,我想做很多手势,但只有一个方法用于操作,具体取决于手势的名称。在调试模式下,我可以清楚地看到_name变量,但如果我尝试编写这样的代码:theRecognizer._name,我被告知在UIGEestureRecognizers中没有“_name”属性。如果我将方法更改为:
-(void)didRecognize_myRecognizer:(myRecognizer*)theRecognizer
{
//do stuff here
}
同样的事情发生了。并且,无论如何这都不会好,因为我想对我制作的所有手势识别器使用该方法。我一无所知吗?告诉我光明!
答案 0 :(得分:0)
首先,您必须为实例变量声明一个访问器。点表示法用于属性而不是实例变量。将name
声明为属性。
然后,您可以检查手势识别器的类并提取名称。
NSString * name = nil;
if ( [theRecognizer isMemberOfClass:[myRecognizer class]] ) {
name = [(myRecognizer *)theRecognizer name];
}