ios - 为什么 touchDragExit 和 touchDragEnter 被多次重复调用?

标签 ios swift uibutton

UIControlEventTouchDragExit

"an event where a finger is dragged from within a control to outside its bounds"

UIControlEventTouchDragEnter

"an event where a finger is dragged into the bounds of the control"

如果我模拟持续向下拖动,基本上退出控件边界一次,为什么多次调用 touchDragExittouchDragEnter

touch drag enter exit gif

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let btn = CustomButton(frame: CGRect(x: 100, y: 100, width: 100, height: 100), image:UIImage())
        btn.setTitle("", for: .normal)
        btn.backgroundColor = UIColor.green
        self.view.addSubview(btn)
    }
}

class CustomButton: UIButton {

    init(frame: CGRect, image:UIImage?) {
        super.init(frame: frame)
        self.addTargets()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    private func addTargets() {
        self.addTarget(self, action: #selector(self.touchDown), for: UIControlEvents.touchDown)
        self.addTarget(self, action: #selector(self.touchUpInside), for: UIControlEvents.touchUpInside)
        self.addTarget(self, action: #selector(self.touchDragExit), for: UIControlEvents.touchDragExit)
        self.addTarget(self, action: #selector(self.touchDragEnter), for: UIControlEvents.touchDragEnter)
        self.addTarget(self, action: #selector(self.touchCancel), for: UIControlEvents.touchCancel)
    }

    func touchDown() {
        print("touched down")
        UIView.animate(withDuration: 0.05, animations: {
            self.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
        },completion: nil)
    }

    func touchUpInside() {
        print("touch up inside")
        UIView.animate(withDuration: 0.7, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 9.0, options: [.curveEaseInOut, .allowUserInteraction], animations: {
            self.transform = CGAffineTransform.identity

        }, completion: nil)
    }

    func touchDragExit() {
        print("touch drag exit")
        UIView.animate(withDuration: 0.7, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: [.curveEaseInOut], animations: {
            self.transform = CGAffineTransform.identity

        }, completion: nil)

    }

    func touchDragEnter() {
        print("touch drag enter")
        UIView.animate(withDuration: 0.05, animations: {
            self.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
        },completion: nil)
    }

    func touchCancel() {
        print("touch canceled")
        UIView.animate(withDuration: 0.05) {
            self.transform = CGAffineTransform.identity
        }
    }

}

最佳答案

将手指末端的大小与像素大小(尤其是在 Retina 显示屏上)进行比较。这种相对差异存在很大的错误空间。操作系统必须做出一些估计才能准确地确定您的手指在屏幕上“指向”的位置,并且当您摆动手指时,估计可能会略有变化。因此,弄清楚您的手指是在一个像素边界的内部还是外部可能有点困难,但有些波动是合理的。

关于ios - 为什么 touchDragExit 和 touchDragEnter 被多次重复调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43750801/

相关文章:

swift - 按下时如何更改 UIButton 的颜色?

ios - viewForAnnotation 没有被解雇

ios - POST 请求响应后启动 segue

function - 在定义之前在 Swift 中声明一个函数

ios - 我如何才能在没有文本的情况下在键盘上按下下一个按钮?

iphone - 如何更改 UITableViewCells 内的 UIButtons

swift - 如何以编程方式在 SWIFT 中设置 UIImage 的 alpha?

ios - 在 Swift 中从属性检查器列表更改栏按钮图像

ios - 占位符在构建时删除约束

swift - 在 Swift 中,如何在 textView 中打印 NSMutableArray