Можно ли настроить область у кнопки, при которой считается .touchDragExit (или .touchDragEnter) (вне ее выбираемой области?)?
Если быть более конкретным, я говорю о такой ситуации: я нажимаю на UIButton, вызывается .touchDown, затем я начинаю отводить палец от кнопки, и в какой-то момент (на некотором расстоянии) он больше не выбирает (и конечно, я могу перетащить обратно, чтобы выбрать...). Я бы хотел изменить это расстояние...
Это вообще возможно?
@Dean, как именно мне реализовать приведенный выше ответ Swift (это старая версия Swift)





Вам нужно перезаписать функции UIButtoncontinueTracking и touchesEnded.
Адаптируя ссылку @Dean, реализация будет следующей (быстро 4.2):
class ViewController: UIViewController {
@IBOutlet weak var button: DragButton!
override func viewDidLoad() {
super.viewDidLoad()
}
}
class DragButton: UIButton {
private let _boundsExtension: CGFloat = 0 // Adjust this as needed
override open func continueTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
let outerBounds: CGRect = bounds.insetBy(dx: CGFloat(-1 * _boundsExtension), dy: CGFloat(-1 * _boundsExtension))
let currentLocation: CGPoint = touch.location(in: self)
let previousLocation: CGPoint = touch.previousLocation(in: self)
let touchOutside: Bool = !outerBounds.contains(currentLocation)
if touchOutside {
let previousTouchInside: Bool = outerBounds.contains(previousLocation)
if previousTouchInside {
print("touchDragExit")
sendActions(for: .touchDragExit)
} else {
print("touchDragOutside")
sendActions(for: .touchDragOutside)
}
} else {
let previousTouchOutside: Bool = !outerBounds.contains(previousLocation)
if previousTouchOutside {
print("touchDragEnter")
sendActions(for: .touchDragEnter)
} else {
print("touchDragInside")
sendActions(for: .touchDragInside)
}
}
return true
}
override open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch: UITouch = touches.first!
let outerBounds: CGRect = bounds.insetBy(dx: CGFloat(-1 * _boundsExtension), dy: CGFloat(-1 * _boundsExtension))
let currentLocation: CGPoint = touch.location(in: self)
let touchInside: Bool = outerBounds.contains(currentLocation)
if touchInside {
print("touchUpInside action")
return sendActions(for: .touchUpInside)
} else {
print("touchUpOutside action")
return sendActions(for: .touchUpOutside)
}
}
}
Попробуйте изменить значение _boundsExtension
Область перетаскивания точно равна области, определяемой границами. Поэтому, если вы хотите настроить перетаскивание, просто настройте границы вашей кнопки.
И как бы я изменил границы UIButton
Я думаю, что следующее может помочь: stackoverflow.com/a/38335868/35499