I am confused where and how to instantiate variables in the handler for a UILongPressGestureRecognizer (LPGR).
I create the LPGR with:
-(void) createLongPress {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
[self.view addGestureRecognizer:longPress];
}
Since the handler is called over and over, however, I'm not sure how and where to instantiate variables.
The following code works but I'm concerned it is instantiating variables over and over. Should I move them into the
case UIGestureRecognizerStateBegan: state
which, I believe only gets called once, or is it okay to leave the at the top of the method which gets called over and over.
Here is the code in my handler
- (IBAction)handleLongPress:(id)sender {
//ALL THIS CODE IS CALLED OVER AND OVER
//Does this mean these variables are getting instantiated over and over?
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState state = longPress.state;
CGPoint location = [longPress locationInView:self.view];
switch (state) {
case UIGestureRecognizerStateBegan: {
//Code called just once at beginning
//DO SETUP }
break;
}
case UIGestureRecognizerStateChanged: {
//Update stuff
}
break;
}
case UIGestureRecognizerStateEnded: {
//cleanup
}
default: {
//handle default case
}
break;
}
}