3

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

Community
  • 1
  • 1
illuminatus
  • 1,986
  • 4
  • 16
  • 19

4 Answers4

0

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.

https://stackoverflow.com/a/45496010/199966

Jlam
  • 1,932
  • 1
  • 21
  • 26
0

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.

MHC
  • 6,405
  • 2
  • 25
  • 26
  • I agree with that, but I'm thinking of an alternate method - subclass UIApplication and overiding its event handling methods – illuminatus Feb 10 '11 at 11:34
  • You didn't get the point. You deal with events in a direct or indirect subclass of UIResponder. The system delivers events to it's window object (linked to the application delegate via window property which in turn dispatches them to each view. So the window is where you see ALL the events in the first place. In other words, it's the most efficient way of getting what you want. – MHC Feb 10 '11 at 17:33
0

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.

mtmurdock
  • 12,756
  • 21
  • 65
  • 108
-1

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.

Sat
  • 1,616
  • 3
  • 22
  • 40
  • Thanks...but the problem is how to implement it in app delegate class.I know that it could be done in view controllers.But it would be tedious to write in all view controllers – illuminatus Feb 10 '11 at 03:42
  • App delegate is not an UIResponder subclass. There is no visual representation of the delegate on the screen, so what you're suggesting makes no sense. Just detect the touch from the window, but then you will have to process all the touches yourself. – futureelite7 Feb 10 '11 at 06:42