gpt4 book ai didi

ios - 在 5 个像素而不是默认的 1 个像素后触摸拖动到 UIButton 内

转载 作者:行者123 更新时间:2023-11-28 12:49:36 26 4
gpt4 key购买 nike

我在 Storyboard上有一个 UIButtonUIButton 连接到 Touch Drag Inside 操作 myTouchDragInsideAction。当用户从内部拖动按钮时触发该操作 (UIControlEventTouchDragInside)。

问题是在内部拖动 1 个像素后触发 Action 。但是 1 个像素太敏感了,手指轻轻一动就可以触发。

@IBAction func myTouchDragInsideAction(sender: UIButton) {

print("Button dragged inside")

}

问题:

我如何扩展此操作以在移动至少 5 个像素后触发内部拖动操作?

最佳答案

您必须为其创建自定义按钮。关注 CustomButton 可能会对您有所帮助。

let DelayPoint:CGFloat = 5

class CustomButton: UIButton {

var startPoint:CGPoint?

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

if self.startPoint == nil {
self.startPoint = touches.first?.previousLocationInView(self)
}


if self.shouldAllowForSendActionForPoint((touches.first?.locationInView(self))!) {
super.touchesMoved(touches, withEvent: event)
}
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.startPoint = nil
super.touchesEnded(touches, withEvent: event)
}

func shouldAllowForSendActionForPoint(location:CGPoint) -> Bool {

if self.startPoint != nil {

let xDiff = (self.startPoint?.x)! - location.x
let yDiff = (self.startPoint?.y)! - location.y

if (xDiff > DelayPoint || xDiff < -DelayPoint || yDiff > DelayPoint || yDiff < -DelayPoint) {

return true
}
}
return false
}
}

您可以根据您的要求更改“DelayPoint”。希望这对您有所帮助。

关于ios - 在 5 个像素而不是默认的 1 个像素后触摸拖动到 UIButton 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36886801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com