I am trying to create a custom gesture recogniser for my view. I am following this answer mentioned in here: But for some reason the touched Ended and also touches movied are not getting called. Only touches began get called.
SubClass:
#import "TapGesture.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
#implementation TapGesture
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (self.state == UIGestureRecognizerStatePossible) {
self.state = UIGestureRecognizerStateRecognized;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
self.state = UIGestureRecognizerStateFailed;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
self.state = UIGestureRecognizerStateFailed;
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
}
#end
and I am initialising the TapGesture as follows:
TapGesture *tapGesture=[[TapGesture alloc]initWithTarget:self action:#selector(incrementValue)];
tapGesture.delaysTouchesEnded=NO;
tapGesture.cancelsTouchesInView=NO;
[_workButton addGestureRecognizer:tapGesture]; //_workButton is a UIView
I don;t have any other gesture recognisers in the view. If I use the same methods in the UIView, all of them are called as expected.
Why the touchesEnded/touchesMoved are not getting called when overrride in UIGestureRecogniser class ?
When subclassing UIGestureRecognizer you must make it act like a continuous gesture and handle it's state machine by yourself (i.e., manually set it's state).
From the iOS Developer Library Docs on UIGestureRecognizer:
Subclasses must set the state property to the appropriate value when they transition between states.
See here for more info (Scroll down to Subclasing Notes)
Note: to make state read/write and not read-only, you should use UIGestureRecognizerSubclass.h as also noted on the docs:
The state property is declared in UIGestureRecognizer.h as being read-only. This property declaration is intended for clients of gesture recognizers. Subclasses of UIGestureRecognizer must import UIGestureRecognizerSubclass.h. This header file contains a redeclaration of state that makes it read-write.
I found that this is required for a double tap gesture but not for a single tap gesture:
doubleTapGestureRecognizer.delaysTouchesEnded = NO;
Related
I have a view being added as a subView of a viewController.
Both this subview and viewController all implement this method
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
I found that when I tapped the subView, both these method will be called.
I want to call subview's touchesEnded only. How to achieve this nicely? (not to add a gesture in it)
In the touchesEnded, apple doc says this "If you override this method without calling super (a common use pattern), you must also override the other methods for handling touch events, if only as stub (empty) implementations."
what's the other method ?
You are close!
To prevent to pass touch event to superview, you should override all the methods for the touch events. Add all the touch event methods to your subview, then you should be OK.
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
Instead of implementing the subview's touchesDidEnd why not do the whole work in the superview's touchesDidEnd something like this
if([touch anyObject].view == subview){
return;
}
This way you will be able to know whether the touch was originally from the subview or the superview.
Alternative : You can implement both the methods in the superview and subview, but like the above code you can return the call in the superview, if the view it interacted with is the subview, and keep working in the touches code in the subview
If in case you want a different opinion plz let us know what you are trying so that we can give the answer accordingly
I am trying to create a custom swipe gesture recognizer to set allowed angles and distances.
So far it works really well but needs a lot of code in the view controller. So my question is wether it is possible to somehow pass a method call like:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
in the view controller to my gesture recogniser object so that I don't have to write something like this
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.swipeGestureRecognizer touchesBegan:touches withEvent:event];
}
into my view controller. I think the build in UIGestureRecognizer does this somehow but I could not figure out how it works.
I really would appreciate your help.
[EDIT]
Generic example: I have an object A which creates an object B. A has a method c that gets called sometimes. What I want to achieve is that a method d of B gets called when c in A gets called. So to somehow forward or pass this method call.
Specific example: The following method gets called in my view controller every time there is a touch on the screen:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
but I actually want my GestureRecognizer object to process this information so I have the same method in my GestureRecognizer object and I want this method to get called every time the counterpart in the view controller gets called.
Sub class UIGestureRecognizer and create a class called CustomGestureRecognizer.
Its .m file will look like this
#implementation CustomGestureRecognizer
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// do your code here
}
#end
Some where in the subclass you have to change the state of the recognizer to
UIGestureRecognizerStateRecognized
when the current gesture is registered, else change to
UIGestureRecognizerStateFailed
.
Now in the view controller where you want to use this recognizer do :
CustomGestureRecognizer * recognizer = [[CustomGestureRecognizer alloc] initWithTarget:self action:#selector(handleChange:)];
recognizer.delegate = self;
[self.view addGestureRecognizer:recognizer];
Detailed explanation is given here Apple doc, under subclassing.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.swipeGestureRecognizer touchesBegan:touches withEvent:event];
}
The above will end up with Recursive calls, and your program will crash at some point of time.
I am overriding hitTest:withEvent to return self (the bottom most view)-
When returning self - my view will respond to touch events in turn initiating gesture recognizers.
If a gesture is canceled or some set of conditions happened - I want to manually initiate hitTest:withEvent and then return a different view to take care of the same sequence of events/touches that occurred. This is necessary as a gesture recognizer only initiates after hitTest:withEvent returns the gestures view and its state changed to began.
I am not sure how to do this - I thought about manually calling on my subviews
- (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
{
}
But I don't have the event parameter (The gesture received it)
I think this could not be done, pass touch event to UIGestureRecognizer is private API. But you can pass touch event the bottom most view received to any view you like and do your own gesture recognize.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIView* selectView = [self _findMatchView];
// maybe convert touches to selectView coordinate
[selectView handleTouchBegan:touches withEvent:event];
}
I am handling Touches delegates and UITapGestureRecognizer for single tap. Following are the methods. When I single tap on my View, I get three events in order of TouchesBegan, SingleTap, and TouchesEnded. Is there anyway to stop TouchesEnded event when I have SingleTap detected? Basicaaly I don't want to perform tasks in TouchesEnded if TouchesMoved didn't happen. I have done this using failTouchEnded BOOL variable in SingleTap and checking it in TouchesEnded but not sure if it is a good idea!
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
failTouchEnded = NO;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(failTouchEnded) return;
}
- (void)GestureView:(GestureView*)view handleSignleTap:(UITapGestureRecognizer*)recognizer {
failTouchEnded = YES;
}
gestureRecognizer has a property cancelsTouchesInView. if it is YES (which is default) cancels the touches if the gesture recognizer recognizes the gesture. So you will get a touches canceled instead of touches ended. You should set delaysTouchesEnded to YES too if it is not YES
Is there a way I can query a UIView to determine if it currently being touched? The reason I ask is because I am using "touchedBegan", "touchesEnded", and "touchesMoved" to keep a UIView under a user's finger. However, if the user moves his/her finger really fast and manages to "escape" the window I want to remove that window.
I was thinking I could use a timer to periodically test each view to determine if it is currently being touched, if it is not, I will remove it. An "IsTouchedProperty" would be perfect.
Any thoughts?
I had a similar problem and solved it by using the tracking property. From the documentation:
A Boolean value that indicates whether the receiver is currently
tracking touches related to an event. (read-only)
This is a method on UIControl, but aren't you trying to create one?
You can also hitTest views. Check the apple docs
Since UIView doesn't inherit from UIControl you would need to subclass UIView and roll your own isTouching property using touch events. Something like:
// MyView.h
#interface MyView : UIView
#property (nonatomic, assign) BOOL isTouching;
#end
// MyView.m
...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
self.isTouching = YES;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
self.isTouching = NO;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
self.isTouching = NO;
}