Reputation: 386
I am looking for a way to forcibly change the state of a UILongPressGestureRecognizer
to UIGestureRecognizerStateEnded
to create my own "maximum duration" extension to the UILongPressGestureRecognizer
(basically to create a sort of touch and hold time up functionality).
Here is my attempt at this below:
- (void)handleLongPressGestures:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
{
NSLog(@"Hold Gesture Started");
timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timesup:) userInfo:nil repeats:NO];
//perform a function - i.e. start something
}
else if(sender.state == UIGestureRecognizerStateEnded)
{
NSLog(@"Hold Gesture Complete");
[timer invalidate];
timer = nil;
//perform a function - i.e. stop something
}
}
-(void)timesup:(NSTimer *)timer {
NSLog(@"Timer Up!");
[longpress setState:UIGestureRecognizerStateEnded];
}
The trouble is that [longpress setState:UIGestureRecognizerStateEnded];
although recommended in xcode, keeps throwing this error:
No visible @interface for 'UILongPressGestureRecognizer' declares the selector 'setState:'
Any ideas on how to resolve this/circumvent this issue? I tried looking for a solution on SO but I haven't found anything. That being said, I apologise if this a duplicate to a question that has already been answered - I just haven't seen it (I promise!)
Upvotes: 2
Views: 881
Reputation: 17132
Mick McCallum's answer works perfectly for Swift 3 as well.
You need to #import <UIKit/UIGestureRecognizerSubclass.h>
into your Project-Bridging-Header.h
file.
Then you're able to set the state like:
var longPressRecognizer: UILongPressGestureRecognizer!
self.longPressRecognizer.state = .ended
Upvotes: 1
Reputation: 130193
You're getting this error because by default, when you import UIKit into your implementation file, it imports UIGestureRecognizer.h, which publicly declares its state property as readonly. If you want to be able to set this property, you have to import UIGestureRecognizerSubclass.h, which redeclares this property as readwrite. Be warned, this is meant for use within a subclass of UIGestureRecognizer, and I don't know enough about how this works under the hood to say this usage is safe.
#import <UIKit/UIGestureRecognizerSubclass.h>
Upvotes: 5