Without subclassing a UIResponder class(e.g. UIView) and overriding its touchesBegan:withEvent: method, is it possible to get informed of touch events of a UIResponder class?
This question was answered here.
Your question is somewhat confusing...'without subclassing UIResponder...can you get informed of touch events of UIResponder?' Are you asking if you can add touchesBegan/Moved/Ended/Cancelled events to any NSObject?!? I'm not comfortable enough with the framework yet to know for sure if those methods would be called if they were implemented as a category on or subclass of NSObject?!?
But, regardless of what you're doing, the top (end) of the responder chain is UIApplication (also a subclass of UIResponder) and if you have an iPhone app then you have a singleton of UIApplication which you can query for touch events...
Related
Is this possible in iOS, or do I really have to register a shake event detection in every single view controller?
I want that a user of my app can shake his iPhone to return to the root. Regardless of the current view controller.
I've done this by writing a category on UIViewController. This way you don't need to subclass anything, you can implement it even if the user interface has been finished already.
In my implementation, the VCs will respond to the shake unless the specific VC has opted out of the mechanism. Opting out is done by setting a BOOL ivar to NO in the specific implementation. The shake mechanism will look for this ivar using key-value-coding and ignore the shake if the ivar has been set.
I'd further refine this by allowing only the VC to respond which is currently visible.
edited post (old information was wrong)
in your case I would write my own ViewController which subclasses UIViewController, implements the motion-delegates of UIResponder and will then call the popToRootViewController on the navigationController-property of the ViewController. And everytimes you create a new ViewController you should subclass your ViewCOntroller and not UIViewController. So every ViewController is able to receive the shake-event but it is only written once in your code :)
This is just a guess, but maybe you could do it by subclassing UIApplication (not your app delegate, the actual application). UIApplication is a UIResponder, so you can make it the first responder, and provide a motionBegan or motionEnded method on it.
Can i create custom touch events for iPhone?? will the device support for creating my own touch event handling ?
Take a look at the UIResponder class:
http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html
You will probably want to implement the touchesBegan:withEvent:, touchesEnded:withEvent: and touchesCancelled:withEvent: methods. These will all be called with an NSSet of UITouches that you can do whatever you want with.
As of the 3.2 SDK there are UIGestureRecognizer classess. They do not play well with the older UIResponder calls, but if you can do 3.2 only they are easier to get going.
There are a number of handy subclasses, such as UIRotationGestureRecognizer for handling rotation.
If you are extending Apple classes like UIScrollView you must use gesture recognizers with 3.2 and later because when a gesture recognizer cancels it will cancel your UIResponder call tracking as well. If you are handling all the gesture tracking this is not an issue.
I have a UIView subclass that overrides UIResponder's touchesMoved: message. I've noticed that when I swipe my finger very quickly across the UIView, my touchesMoved: message only gets called every so often and not constantly getting messaged.
Any ideas?
Thanks.
According to Apple Developer Technical Support this is by design.
I'm working in a class that is a delegate of another class; this other class is in a third-party library that I'd like to avoid changing if at all possible.
The original class is handling a -touchesEnded event, and is delegating responsibility to me, but it doesn't pass the UIEvent itself to me. Is there a way to get a handle to the current event in progress? It's a double-tap handler, and all that is passed to me is the view being touched, and the point at which the touch occurred. I want to know exactly how many touches are involved in the event.
Thanks!
randy
You can subclass the original 3rd-party view and override -touchesEnded:withEvent: in that subclass.
You can also subclass UIApplication and override its -sendEvent: method to capture all (public) events.
I am developing an iPhone application with multiviews (Nav controller), but i like to receive an event if user touches in any view of the view. I understand it can be done by subclassing application delegate? If that's true how can i do it? My requirement is, i like to receive an event as soon as user touches any where in any view within my application.
Thanks for your help and time.
Your reference to subclassing UIApplication will work. Read down through the comments and it covers a somewhat quirky IMO way to implement it (by having the AppDelegate be a subclass of UIApplication). Myself, I would create a separate class to be the UIApplication subclass, rather than having the app delegate do both jobs, but I see the merit of either way.
That said, this is a very large and unusual stick and may suggest a design failure. What problem are you solving with this?
A way to do it is to use a Singleton class (which acts as an observer/mediator), which the application is an example of, in which you have viewControllers subscribe to when they are intersted in the touch events of a certain view. When the touch occurs the Singleton class is informed of the event as a result it informs all subscribers to the event of the event.
Here is an example
#interface MyEventClass
{
-(void)TouchEventDidOccur;
-(void)subscribeToTouchEvent:(id)delegate selector(selector):sel
}
Above is the singleton class
now this is an example of what the view touchesBegan method might look like
-(void)touchesBegan...
{
[[MyEventClass sharedInstance] TouchEventDidOccur];
}
and how one would subscribe to the event
[[MyEventClass sharedInstance] subscribeToTouchEvent:self selector:#selector(receiveTouchEvent:)]
hope this helps
What's wrong with using notifications? If you have disconnected classes across your application, it's trivial to have them listen for a particular notification, then have your views or view controllers post that notification when a touch event happens. All of the observers will take action on the notification.