简体   繁体   English

Swift iOS-检测触摸拖动从UIView外部输入UIView

[英]Swift iOS - Detect Touch Drag Enter UIView from outside UIView

How do I get an event for a Touch, anywhere on the screen, that is draged and enters a specific UIView? 如何在屏幕上的任何地方获取一个事件,将其拖动并进入特定的UIView?

How can I do it, in a simple way, in Swift for iOS? 如何在iOS的Swift中以一种简单的方式做到这一点?

An example of what I want to achieve is shown below: 我想要实现的示例如下所示:

在此处输入图片说明

Override method of controller 控制器的超越方法

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    }

than check if any touch is inside of your views rect using function CGRectContainsPoint 然后使用函数CGRectContainsPoint检查是否在视图内部是否有任何触摸

You can add a button on the top of yourView and add touchDrag Inside control to it. 您可以在yourView的顶部添加一个按钮,并向其添加touchDrag Inside控件。

let button = UIButton(frame: yourView.bounds)
button.addTarget(target: self, action:#selector(self.yourmethodName), forControlEvents: .TouchDragInside)

Apple Documentation Reference : Apple文档参考

TouchDragInside An event where a finger is dragged inside the bounds of the control. TouchDragInside一个事件,在该事件中手指在控件的边界内拖动。

Available in iOS 2.0 and later. 在iOS 2.0及更高版本中可用。

TouchDragOutside An event where a finger is dragged just outside the bounds of the control. TouchDragOutside一个事件,在该事件中手指被拖动到控件的边界之外。

Available in iOS 2.0 and later. 在iOS 2.0及更高版本中可用。

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

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