UIButton继续跟踪,即使超出范围

时间:2018-07-24 08:21:05

标签: swift uibutton touchesmoved

我正在尝试跟踪按钮上的触摸。但是只要触摸离开按钮框,它就会停止一起跟踪触摸。

我尝试使用:

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    let relativeFrame = bounds
    let hitTestInsets = UIEdgeInsets(top: -10, left: -10, bottom: -10, right: -10)

    return UIEdgeInsetsInsetRect(relativeFrame, hitTestInsets).contains(point)
}

override open func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

    let minimalWidthAndHeight: CGFloat = 60

    let buttonSize = frame.size
    let widthToAdd = (minimalWidthAndHeight - buttonSize.width > 0) ? minimalWidthAndHeight - buttonSize.width : 0
    let heightToAdd = (minimalWidthAndHeight - buttonSize.height > 0) ? minimalWidthAndHeight - buttonSize.height : 0
    let largerFrame = CGRect(x: 0-(widthToAdd / 2), y: 0-(heightToAdd / 2), width: buttonSize.width + widthToAdd, height: buttonSize.height + heightToAdd)
    return largerFrame.contains(point) ? self : nil
}

我也尝试使用continueTracking(with:),但是在所有情况下,只要触摸移离按钮框,就会调用cancelTracking(with:)touchesCancelled(with:)

即使触摸离开按钮框,我仍要怎么做才能使其保持跟踪?

0 个答案:

没有答案