对于一些UIViews到他们的整体控制器点击

对于一些UIViews到他们的整体控制器点击

本文介绍了Swift,对于一些UIViews到他们的整体控制器点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DotBoss:UIViewController



现场有十几个UIView,点:UIView



(有些是直接的子视图,有些是进一步的。)



在最高控制器和Dot项目之间的链中,甚至还有容器视图



十几个点项目知道他们是否被点击...

  class Dot:UIView 
{
private var tap:UITapGestureRecognizer? = nil
覆盖func awakeFromNib()
{
tap = UITapGestureRecognizer(target:self,
action:#selector(SnapDot.handleTap(_ :)))
self.addGestureRecognizer(tap!)
}
func handleTap(g:UITapGestureRecognizer)
{
打印(用户点击此特定点...)

}

我想要 DotBoss 知道何时点击 Dot 之一。

  class DotBoss:UIViewController 
{
func oneDotWasClicked(d:Dot)
{
}
}

如何在Swift中完美完成?






注意这个冒泡扩展名非常有用: https://blog.veloxdb.com/2016/05/12/bubbling-一个点(UIView),它最终控制着DotBoss(UIView) UIViewController)都是响应者,并且是响应者链之间的链接。因此,Dot可以通过应答器链在DotBoss中调用方法直到DotBoss:

  func handleTap(g:UITapGestureRecognizer){
// ...如果需要,其他东西可以去这里...
var r:UIResponder = self
repeat {r = r .nextResponder()! } while!(r是DotBoss)
(r as!DotBoss).oneDotWasClicked(self)
}


I have a DotBoss:UIViewController

There are a dozen UIView in the scene, Dot:UIView

(Some are direct subviews, some are further down.)

There are even container views in the chain between the highest controller and the Dot items.

The dozen Dot items know if they are tapped...

class Dot:UIView
    {
    private var tap:UITapGestureRecognizer? = nil
    override func awakeFromNib()
        {
        tap = UITapGestureRecognizer(target:self,
                     action: #selector(SnapDot.handleTap(_:)))
        self.addGestureRecognizer(tap!)
        }
    func handleTap(g:UITapGestureRecognizer)
        {
        print("user tapped on this particular Dot...")
        }
    }

I want DotBoss to know when one of the Dot is tapped.

class DotBoss:UIViewController
  {
  func oneDotWasClicked(d:Dot)
   {
   }
  }

How to do this perfectly in Swift?


Note for anyone googling here, this bubbling extension is remarkably useful: https://blog.veloxdb.com/2016/05/12/bubbling-events-using-uiresponder-in-swift/

解决方案

A Dot (UIView) and its ultimately controlling DotBoss (UIViewController) are both responders, and are links along the responder chain. Therefore, a Dot can call a method in its DotBoss by walking the responder chain until it comes to the DotBoss:

func handleTap(g:UITapGestureRecognizer) {
    // ... other stuff can go here if necessary ...
    var r : UIResponder = self
    repeat { r = r.nextResponder()! } while !(r is DotBoss)
    (r as! DotBoss).oneDotWasClicked(self)
}

这篇关于Swift,对于一些UIViews到他们的整体控制器点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:24