I want to catch touch event in all my views.So it should deal with appdelegate
.
I referred
iPhone: Detecting user inactivity/idle time since last screen touch
but was not success
Hope I'll get in right direction
Thanks
I want to catch touch event in all my views.So it should deal with appdelegate
.
I referred
iPhone: Detecting user inactivity/idle time since last screen touch
but was not success
Hope I'll get in right direction
Thanks
No need to subclass UIWindow, simple passthrough gesture sample code here, catching all touches for all views:
There's a way to do this app wide without individual controllers having to do anything. Just add a gesture recognizer that doesn't cancel touches. This way, all touches will be tracked for the timer, and other touches and gestures aren't affected at all so no one else has to know about it.
fileprivate var timer ... //timer logic here
@objc public class CatchAllGesture : UIGestureRecognizer {
override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
}
override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
//reset your timer here
state = .failed
super.touchesEnded(touches, with: event)
}
override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesMoved(touches, with: event)
}
}
@objc extension YOURAPPAppDelegate {
func addGesture () {
let aGesture = CatchAllGesture(target: nil, action: nil)
aGesture.cancelsTouchesInView = false
self.window.addGestureRecognizer(aGesture)
}
}
In your app delegate's did finish launch method, just call addGesture and you're all set. All touches will go through the CatchAllGesture's methods without it preventing the functionality of others.
App delegate is not a responder. I would subclass UIWindow, and override it's event handling methods, because window gets all touch events in the first time.
Contrary to what futureelilte7 says, your application delegate (generated when the project is created) is in-fact a subclass of UIResponder and responds to the selector touchesBegan:withEvent:
and other similar selectors.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
// your code here
}
This should preserve the default touch behavior, and give you a chance to do your own custom functionality.
just implement
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
//your logic
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//your logic
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//your logic
}
in your code..I think rest of the think you can figured out.