Reputation: 2361
So I am relatively new to Objective-C and there is a definite possibility I am missing something fairly obvious, so feel free to throw tomatoes at me if so. Here is my problem:
I have an iPhone single-view application consisting of one MapView
. I included a long-press gesture recognizer on that MapView
in order to annotate/drop a pin. It appears that this will work if I do not specify a minimumPressDuration
...
- (void)viewWillAppear:(BOOL)animated {
UILongPressGestureRecognizer * longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[mapView addGestureRecognizer:longPressRecognizer]; // Actual map view handler is added to
...
}
-(void) handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
NSLog (@"Did it work?!");
// Some code here to make pins appear; omitted for simplicity in this question
}
As expected, when I long press the map, after 0.5
seconds (the default time) I will have 1 pin on the map. If I move around my finger, I have a crapload of pins. The log also prints out "Did it work?!" many times over and over. Again, expected. However, when I add one little line to the viewWillappear method (right before messaging mapView's addGestureRecognizer
):
longPressRecognizer.minimumPressDuration = 1.0;
...the gesture no longer works. In that I hold press for 1 second and nothing appears on the log. Let go, try for longer than one second... nothing. No pins are dropped, and nothing prints out to the log. Why is this occurring? I can't understand why setting a minimum duration would break this. My expectation is that this should function the same as before - no custom-set minimum press duration - with the only difference being I need to hold down for 1 full second instead of half of one.
Update: it appears that if I choose values between 0.1
and 0.5
inclusive it works. But once I go to 0.6
or greater it does not work. Something going on under the hood in Obj-C I don't know about?
Also, I know I should be checking state in my handler function, so unless that will fix that problem don't pile on me for it. I just want to get the bare bones working first before I delve into the details.
Thanks!
P.S., using OS X 10.8.2 and Xcode 4.5.1 (4G1004).
Upvotes: 0
Views: 5738
Reputation: 566
I encountered the same issue. After set recognizer's delegate and turn simultaneous recognition on, this issue is solved. Following is my code for your reference:
- (void)init
{
UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lockLongPress:)];
longPressGestureRecognizer.minimumPressDuration = 1.0;
longPressGestureRecognizer.delegate = self;
[targetView addGestureRecognizer:longPressGestureRecognizer];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Upvotes: 3