【问题标题】:Why are touchDragExit and touchDragEnter being called repetitively multiple times?为什么 touchDragExit 和 touchDragEnter 会被多次重复调用?
【发布时间】:2017-05-03 03:58:27
【问题描述】:

UIControlEventTouchDragExit

"一个手指从控件内部拖到外部的事件 它的界限”

UIControlEventTouchDragEnter

“手指被拖入控件边界的事件”

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

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
        }
    }

}

【问题讨论】:

    标签: ios swift uibutton


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 2022-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      相关资源
      最近更新 更多