How to catch touch event in all applications?

I want to catch the touch event in all my eyes. Therefore, he must deal with appdelegate.

I called

iPhone: Detect user inactivity / downtime since last touch

but was not successful

I hope I go in the right direction

thank

+3
source share
4 answers

The application delegate is not a responder. I would subclass UIWindow and override its event handling methods because the window receives all touch events for the first time.

0
source

You do not need to subclass UIWindow, here is a simple example of a gesture code, intercepting all the strokes for all views:

- . , . , , , .

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)
    }
}

, addGesture, . CatchAllGesture .

fooobar.com/questions/43306/...

0

- (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

}

. , .

-1

Contrary to what futureelilte7 says, your application delegate (generated when the project was created) is actually a subclass of UIResponder and responds to a 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 enable you to perform your own custom functions.

-1
source

Source: https://habr.com/ru/post/1790955/


All Articles