简体   繁体   中英

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?

How can I do it, in a simple way, in Swift for iOS?

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

You can add a button on the top of yourView and add touchDrag Inside control to it.

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

Apple Documentation Reference :

TouchDragInside An event where a finger is dragged inside the bounds of the control.

Available in iOS 2.0 and later.

TouchDragOutside An event where a finger is dragged just outside the bounds of the control.

Available in iOS 2.0 and later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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