【问题标题】:How to find out if user interact on view controller?如何确定用户是否在视图控制器上进行交互?
【发布时间】:2017-02-17 13:04:17
【问题描述】:

我有一个视图控制器,我使用计时器来关闭这个视图控制器。我有一些按钮,但是当用户使用某些手势时,我需要重置该计时器。所以我必须处理特定视图控制器中的每个手势并在那里调用重置计时器功能。我知道有一种方法可以覆盖 UIApplication 方法(void)sendEvent: 并发送一些广播通知。但我想在视图控制器上处理它。以某种方式捕获所有通知和调用方法。

非常感谢。

【问题讨论】:

    标签: ios view controller gestures interaction


    【解决方案1】:

    UIViewController 继承自 UIResponder 类。

    所以你可以选择从你的问题中实现所有UIResponder类的触摸事件。

    来自Docs:

    func touchesBegan(Set<UITouch>, with: UIEvent?) // Tells this object that one or more new touches occurred in a view or window.
    func touchesMoved(Set<UITouch>, with: UIEvent?) // Tells the responder when one or more touches associated with an event changed.
    func touchesEnded(Set<UITouch>, with: UIEvent?) // Tells the responder when one or more fingers are raised from a view or window.
    func touchesCancelled(Set<UITouch>, with: UIEvent?) // Tells the responder when a system event (such as a system alert) cancels a touch sequence.
    

    希望这会有所帮助。

    【讨论】:

    • 是的,我的错误你提到了 UIResponder :)
    • 感谢您的回复,问题是我的视图控制器中有一些按钮,导航控制器嵌入,搜索..当我点击视图时 touchesBegan 工作,没有按钮或导航栏。我需要以某种方式覆盖整个窗口。
    • 为此,您可以在当前视图(内部有按钮和其他控件)之上添加一个透明视图,然后您可以为新添加的透明视图实现相同的委托方法。
    【解决方案2】:

    UIViewController 有类似

    的方法
     - touchesBegan(_:with:) 
     - touchesEnded(_:with:)
     - touchesMoved(_:with:)
     - touchesCancelled(_:with:)
    

    这能满足您的需要吗?

    【讨论】:

    • 不是UIViewController的方法,是UIView的方法
    【解决方案3】:

    有两种方法可以处理用户在视图上的交互/操作。

    点击手势:使用UITapGestureRecognizer
    UITapGestureRecognizer 是 UIGestureRecognizer 的一个具体子类,用于查找单个或多个点击。要识别手势,指定数量的手指必须点击视图指定的次数。

    func handleTap(sender: UITapGestureRecognizer) {
        if sender.state == .Ended {
            // handling code
        }
    }
    


    触摸:通过使用UITouch(由 UIViewController 继承自 UIResponder
    UITouch 对象表示屏幕上发生的触摸的位置、大小、移动和力度。

    func touchesBegan(Set<UITouch>, with: UIEvent?) // Tells this object that one or more new touches occurred in a view or window.
    func touchesMoved(Set<UITouch>, with: UIEvent?) // Tells the responder when one or more touches associated with an event changed.
    func touchesEnded(Set<UITouch>, with: UIEvent?) // Tells the responder when one or more fingers are raised from a view or window.
    func touchesCancelled(Set<UITouch>, with: UIEvent?) // Tells the responder when a system event (such as a system alert) cancels a touch sequence.
    

    【讨论】:

      猜你喜欢
      • 2020-02-12
      • 2019-04-16
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多