I have made a view clickable with a tap gesture recognizer, which is working just fine. But I want to highlight the view when the touch happens en remove it when the touch e
UITapGestureRecognizer
will never go in the UIGestureRecognizerStateBegan
state. Only continuous gestures (such as a swipe or a pinch) will result for their recognizers going from UIGestureRecognizerStatePossible
to UIGestureRecognizerStateBegan
. Discrete gestures, such as a tap, put their recognizers directly into UIGestureRecognizerStateRecognized
, i.e. for a single tap, right into UIGestureRecognizerStateEnded
.
That said, maybe you're looking for a UILongPressGestureRecognizer
, which is a continuous recognizer that will enter UIGestureRecognizerStateBegan
, allowing you to discern beginning and end of touch?
It might be too late. But this will also help you, if you strictly want to use Gesture recognizer.
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(refresh:)];
longPress.minimumPressDuration = 0.0;
- (IBAction)refresh:(UILongPressGestureRecognizer *)sender {
if(self.currentStatus == NODATA){
if(sender.state == UIGestureRecognizerStateBegan){
NSLog(@"Began!");
[self.dashboardViewController.calendarContainer state:UIViewContainerStatusSELECTED];
}
if (sender.state == UIGestureRecognizerStateEnded){
NSLog(@"%@", @"Ended");
[self.dashboardViewController.calendarContainer state:UIViewContainerStatusNORMAL];
}
[self setState:REFRESHING data:nil];
}
}
Quite an old question, but still. Hope it will be helpful for somebody. If I've got the author's question right, the idea is to detect whether the tap has began recognizing and to perform an action. Like if you don't want target to trigger only when the user releases the finger but already when he touch it for the very first time.
An easy way to do so is to make an extension for UITapGestureRecognizer
like below:
fileprivate class ModTapGestureRecognizer: UITapGestureRecognizer {
var onTouchesBegan: (() -> Void)?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
onTouchesBegan?()
super.touchesBegan(touches, with: event)
}
}
Later in your code you can use it like so:
let tapRecognizer = ModTapGestureRecognizer()
tapRecognizer.addTarget(self, action: #selector(didTapped))
tapRecognizer.onTouchesBegan = {
print("Yep, it works")
}
yourView.addGestureRecognizer(tapRecognizer)
And you're awesome!
You can also use touchesBegan:withEvent:
and touchesEnded:withEvent:
methods.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *t = [event touchesForView:_myView];
if([t count] > 0) {
// Do something
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *t = [event touchesForView:_myView];
if([t count] > 0) {
// Do something
}
}