自定义UIButton .touchDragEnter和.touchDragExit区域/大小?

时间:2019-07-19 22:47:15

标签: ios swift uibutton

是否可以从被认为是.touchDragExit(或.touchDragEnter)(不在其可选区域内)的按钮处自定义区域?

更具体地说,我正在谈论这种情况:我点击UIButton,调用了.touchDown,然后我开始将手指从按钮上拖开,并且在某个点(相隔一定距离)它将不再选择(当然,我可以向后拖动以选择...)。我想修改该距离...

这有可能吗?

2 个答案:

答案 0 :(得分:1)

拖动区域实际上等于由边界定义的区域。 因此,如果要自定义拖动,则只需自定义按钮的边界即可。

答案 1 :(得分:0)

您需要覆盖continueTracking touchesEndedclass 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) } } } 函数。

适应@Dean的链接,实现如下(swift 4.2):

_boundsExtension

尝试更改Ext.define('Test', { extend: 'Ext.Window', requires: [ 'Ext.form.Panel' ], autoShow: true, title: "Test", width: 350, items: [{ //Your content panel xtype: 'panel', padding:20, height: 300, html: 'My Content Panel' }, { //Custom button panel xtype: 'panel', padding: 10, layout: { align: 'middle', pack: 'center', type: 'hbox' }, items: [{ margin: '0 2 0 0', xtype: 'button', text: 'Cancel' }, { margin: '0 2 0 0', xtype: 'button', text: 'Submit' }, { margin: '0 2 0 0', xtype: 'button', text: 'Reset' }] }], }); Ext.create('Test');