简体   繁体   English

如何在应用程序的每个视图中捕捉触摸事件?

[英]How to catch touch event in every view of an app?

I want to catch touch event in all my views.So it should deal with appdelegate .我想在我所有的视图中捕捉触摸事件。所以它应该处理appdelegate

I referred我提到

iPhone: Detecting user inactivity/idle time since last screen touch iPhone:检测自上次屏幕触摸后用户不活动/空闲时间

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:无需子类化 UIWindow,这里是简单的直通手势示例代码,捕获所有视图的所有触摸:

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.在您的应用委托的 did finish 启动方法中,只需调用 addGesture 即可。 All touches will go through the CatchAllGesture's methods without it preventing the functionality of others.所有触摸都将通过 CatchAllGesture 的方法进行,而不会妨碍其他人的功能。

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

App delegate is not a responder. App delegate 不是响应者。 I would subclass UIWindow, and override it's event handling methods, because window gets all touch events in the first time.我会将 UIWindow 子类化,并覆盖它的事件处理方法,因为窗口会在第一时间获取所有触摸事件。

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.与 futureelilte7 所说的相反,您的应用程序委托(在创建项目时生成)实际上是 UIResponder 的子类,并响应选择器touchesBegan:withEvent:和其他类似的选择器。

- (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.在你的代码中..我认为你可以想出其余的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM