开发者

accessing info from a custom gesture recognizer

I have written a simple custom gesture recognizer like this:

@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

and I have initiated it like this:

myRecognizer *recognizer = [[myRecognizer alloc] initWithTarget:self action:@selector(didRecognize_myRecognizer:)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];

so far so good. and I have my action here:

-(void)didRecognize_myRecognizer:(UIGestureRecognizer*)theRecognizer
    {
        //d开发者_Go百科o stuff here
    }

ok, this is working fine so far, and everything is good. But, here is my question, how can I get access to the _name property I created in my custom gesture in the //do stuff here arear? Basically, I would like to make many gestures, but only have a single method used for the actions, depending on the name of the gesture. In debugging mode I can clearly see the _name variable, but if I try writing code like this: theRecognizer._name, I'm told that there is no "_name" property in UIGEestureRecognizers. if I change the method to this:

-(void)didRecognize_myRecognizer:(myRecognizer*)theRecognizer
    {
        //do stuff here
    }

the same thing happens. and, that wouldn't be good anyway because i want to use the method for all of the gesture recognizers i make. am I clueless? show me the light!


First off, you have to declare an accessor for the instance variable. Dot notation is for properties not instance variables. Declare name as property.

Then, You can check the class of the gesture recognizer and extract the name.

NSString * name = nil;
if ( [theRecognizer isMemberOfClass:[myRecognizer class]] ) {
    name = [(myRecognizer *)theRecognizer name];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜