I searched but not quite understand why we cant detect a UITouch on UITableView. What I am having right now is :a view controller with a table view located in its view. Please look at the picture below for your reference
In implementation class, I am enabling breakpoint for each UITouch methods which are
- (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;
I notice that, these breakpoints are invoked if and only if you touch outside of the table view ( orange area )
I do not get it. I thought UITableView is subclass of UIScrollView which is subclass of UIView which is subclass of UIResponder. It means UITouch should be invoked. (correct me if I am wrong )
All comments are welcomed and appreciated here.
Rather than tampering with the table view, use a gesture recognizer. You can act as the delegate to ensure that all interactions work concurrently and enable and disable the gestures if / as required.
You can detect touches method in the UITableView by subclassing it as this:
I Test it and it print "Test" successfully
//TestTable.h
#import <UIKit/UIKit.h>
#interface TestTable : UITableView
#end
//TestTable.m
#import "TestTable.h"
#implementation TestTable
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog("Test");
}
Tables utilize scroll views to handle panning, which use a pan gesture recognizer. Why not just tap into that?
CGPoint location = [self.tableView.panGestureRecognizer locationInView:self.tableView];
If you wish to detect the touches on UITableView, create a subclass of tableview and add implement UIResponder method, canBecomeFirstResponder.
#interface MyTableView: UITableView
#end
#implementation: MyTableView
- (BOOL) canBecomeFirstResponder{
return YES;
}
// then implement all other touch related methods, remember to call super in these methods
// such that it correctly forwards the events to other responders
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
}
#end
Related
I'm building a game that basically the goal is to hold a button pressed for a long time.
The game starts with the "touchesBegan" call, and ends on "touchesEnded".
I have a issue that some very specific times this call, is not called. After a some search i figured out i'm not the only one with this problem :
https://discussions.apple.com/thread/1507669?tstart=0
There is a known problem that some times "touchesEnded" is not called.
So the work around i was thinking of doing, is setting a timer, and checking every once in a while, if there is a finger pressing, and where exactly on the screen.
Problem is, i know only about these methods:
- (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;
And nun of them serve me for this use.
Is there a way of getting the current touch on screen?
Thanks
touchesCancelled:withEvent:
When a touch begins, touchesBegan:withEvent: is called. After that, at some point in the future, either touchesEnded:withEvent: or touchesCancelled:withEvent: will be called! You can, e.g. create a method touchesEndedOrCancelled:withEvent: and call that method from both touchesEnded and touchesCancelled.
To get the current touches you need to keep track of them on your own. Just create a mutable set _allTouches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_allTouches unionSet:touches];
// ...
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[_allTouches minusSet:touches];
// ...
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[_allTouches minusSet:touches];
// ...
}
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'm trying to implement ccTouchesBegan in my GameScene.
I've set isTouchEnabled = YES. I'm also calling addStandardDelegate on the touchDispatcher. Finally, in my AppDelegate i have [glView setMultipleTouchEnabled:YES].
However, ccTouchesBegan is never called.
What am I doing wrong?
Solved it!
I was registering touches on a previous layer, but the layer wasn't being dealloced because you have to "un-register" with the onExit method.
Long story short: touchesBegan was not being called on my GameLayer because it was being swallowed by another layer.
Create one dummy application and try this:
#protocol CCStandardTouchDelegate <NSObject>
#optional
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
#end
May be it will help you..
I subclassed the UIWindow, but for some reason the - (void)sendEvent:(UIEvent *)event gets called 2 times for any interaction with the screen. Any ideas why would that happen?
For debugging purposes, subclass window (of app delegate) and override sendEvent: method
-(void) sendEvent:(UIEvent *)event
{
NSLog(#"%#",event);
[super sendEvent:event];
}
Most probably, you will notice the events responsible for TouchesBegan and TouchesEnded (for tapping). This can be tested by subclassing the View and overriding touch related methods.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"tocuhesBegan");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesMoved");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesEnded");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesCancelled");
}
Also, try drag/swipe on the view to notice the change in count of events sent to view :)
sendEvent gets called for fingerDown and allFingersUp
I am having 3 UIViews stacked one on top of another
UITableview
planeView
rootView
TableView is at the top and rootView at the bottom. (rootView is not visible as TableView is on top of it)
I have implemented the following code in rootView
/*code in rootView*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {}
expecting that these functions will be called when the top most view ie TableView is touched or moved,but on the contrary none of the functions were called.
I also tried putting the following code in TableView so that the rootView methods are called
/*code in TableView so that the rootView methods are called(TableView is the subview of rootView)*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self.superview touchesBegan:touches withEvent:event];
}
As expected it did so but the problem is that the TableView delegates like
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
are not called.
Is there any way by which it can be ensured that the TableView delegates implemented in TableView class(didSelectRow:) and the touchesBegan:,touchesMoved.. functions in rootView are also called accordingly?
ie When i click on a TableCell both (didSelectRow:atIndex) function in--> TableView and (touchesBegan and touchesEnd) method in-->rootView are called.
In your subclass of UITableView you should have touch methods like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.nextResponder touchesBegan:touches withEvent:event];
[super touchesBegan:touches withEvent:event];
}
The difference here is that you're passing the touch to the next responder instead of the superview, and you're doing this before passing the touch to super.
Then in planeView you need to pass touches like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.superview touchesBegan:touches withEvent:event];
}
Keep in mind that this still may not work exactly as you expect. UITableView does a lot of mangling of the responder chain under the hood, in order to make it seem as if a UITableView (which is actually a complex collection of subviews) is just another view like a button or a label.
None of this worked for me
What solved it was simply:
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let view = super.hitTest(point, with: event)
return view == self ? nil : view
}
Ref this article: https://medium.com/#nguyenminhphuc/how-to-pass-ui-events-through-views-in-ios-c1be9ab1626b