Ios 在5个像素(而不是默认的1个像素)后在UIButton内触摸拖动
我在故事板上有一个Ios 在5个像素(而不是默认的1个像素)后在UIButton内触摸拖动,ios,swift,events,uibutton,uicontrolevents,Ios,Swift,Events,Uibutton,Uicontrolevents,我在故事板上有一个ui按钮。ui按钮连接到触摸拖动内部操作myTouchDragInsideAction。当用户从内部拖动按钮时触发该操作(UIControlEventTouchDragInside) 问题是该操作是在内部拖动1像素后触发的。但是,1像素太敏感,只需轻轻移动手指即可触发 @IBAction func myTouchDragInsideAction(sender: UIButton) { print("Button dragged inside") } 问题: 如何扩
ui按钮
。ui按钮
连接到触摸拖动内部
操作myTouchDragInsideAction
。当用户从内部拖动按钮时触发该操作(UIControlEventTouchDragInside
)
问题是该操作是在内部拖动1像素后触发的。但是,1像素太敏感,只需轻轻移动手指即可触发
@IBAction func myTouchDragInsideAction(sender: UIButton) {
print("Button dragged inside")
}
问题:
如何扩展此操作以仅在移动至少5个像素后触发内部拖动操作?您必须为其创建自定义按钮。下面的自定义按钮可能对您有所帮助
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
}
}
let DelayPoint:CGFloat=5
类CustomButton:UIButton{
var起始点:CGPoint?
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
如果self.startPoint==nil{
self.startPoint=触碰.first?.previousLocationInView(self)
}
如果self.shouldAllowForSendActionForPoint((触碰.first?.locationInView(self))!){
super.touchsmoved(touchs,with event:event)
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
self.startPoint=nil
super.touchesend(touchs,withEvent:event)
}
func应允许ForSendActionForPoint(位置:CGPoint)->Bool{
如果self.startPoint!=零{
设xDiff=(self.startPoint?.x)!-location.x
设yDiff=(self.startPoint?.y)!-location.y
if(xDiff>DelayPoint | | xDiff<-DelayPoint | yDiff>DelayPoint | yDiff<-DelayPoint){
返回真值
}
}
返回错误
}
}
根据您的要求更改“延迟点”。
希望这将对您有所帮助。您必须为其创建自定义按钮。下面的自定义按钮可能对您有所帮助
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
}
}
let DelayPoint:CGFloat=5
类CustomButton:UIButton{
var起始点:CGPoint?
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
如果self.startPoint==nil{
self.startPoint=触碰.first?.previousLocationInView(self)
}
如果self.shouldAllowForSendActionForPoint((触碰.first?.locationInView(self))!){
super.touchsmoved(touchs,with event:event)
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
self.startPoint=nil
super.touchesend(touchs,withEvent:event)
}
func应允许ForSendActionForPoint(位置:CGPoint)->Bool{
如果self.startPoint!=零{
设xDiff=(self.startPoint?.x)!-location.x
设yDiff=(self.startPoint?.y)!-location.y
if(xDiff>DelayPoint | | xDiff<-DelayPoint | yDiff>DelayPoint | yDiff<-DelayPoint){
返回真值
}
}
返回错误
}
}
根据您的要求更改“延迟点”。
希望这将对您有所帮助。我的forSwift 3的实现
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
}
}
final class DraggedButton: UIButton {
// MARK: - Public Properties
@IBInspectable var sensitivityOfDrag: CGFloat = 5
// MARK: - Private Properties
private var startDragPoint: CGPoint?
// MARK: - UIResponder
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let firstTouch = touches.first else {
return
}
let location = firstTouch.location(in: self)
let previousLocation = firstTouch.previousLocation(in: self)
if startDragPoint == nil {
startDragPoint = previousLocation
}
if shouldAllowForSendActionForPoint(location: location) {
super.touchesMoved(touches, with: event)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
startDragPoint = nil
super.touchesEnded(touches, with: event)
}
// MARK: - Private methods
private func shouldAllowForSendActionForPoint(location: CGPoint) -> Bool {
guard let startDragPoint = startDragPoint else {
return false
}
let xDifferent = abs(startDragPoint.x - location.x)
let yDifferent = abs(startDragPoint.y - location.y)
return xDifferent > sensitivityOfDrag || yDifferent > sensitivityOfDrag
}
}
最终类DraggedButton:UIButton{
//马克:公共财产
@i可检测的无功灵敏度Fdrag:CGFloat=5
//马克:私人物业
私人var startDragPoint:CGPoint?
//MARK:-UIResponder
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
守卫让第一次触碰=触碰第一次{
返回
}
let location=firstTouch.location(in:self)
让previousLocation=firstTouch.previousLocation(in:self)
如果startDragPoint==nil{
startDragPoint=以前的位置
}
if shouldAllowForSendActionForPoint(位置:位置){
super.touchesMoved(touches,with:event)
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
startDragPoint=nil
super.touchesend(触摸,带有:事件)
}
//MARK:-私有方法
private func应允许ForSendActionForPoint(位置:CGPoint)->Bool{
警卫让startDragPoint=startDragPoint else{
返回错误
}
设xDifferent=abs(startDragPoint.x-location.x)
设yDifferent=abs(startDragPoint.y-location.y)
返回xDifferent>sensitivityOfDrag | | yddifferent>sensitivityOfDrag
}
}
forSwift 3的我的实现
final class DraggedButton: UIButton {
// MARK: - Public Properties
@IBInspectable var sensitivityOfDrag: CGFloat = 5
// MARK: - Private Properties
private var startDragPoint: CGPoint?
// MARK: - UIResponder
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let firstTouch = touches.first else {
return
}
let location = firstTouch.location(in: self)
let previousLocation = firstTouch.previousLocation(in: self)
if startDragPoint == nil {
startDragPoint = previousLocation
}
if shouldAllowForSendActionForPoint(location: location) {
super.touchesMoved(touches, with: event)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
startDragPoint = nil
super.touchesEnded(touches, with: event)
}
// MARK: - Private methods
private func shouldAllowForSendActionForPoint(location: CGPoint) -> Bool {
guard let startDragPoint = startDragPoint else {
return false
}
let xDifferent = abs(startDragPoint.x - location.x)
let yDifferent = abs(startDragPoint.y - location.y)
return xDifferent > sensitivityOfDrag || yDifferent > sensitivityOfDrag
}
}
最终类DraggedButton:UIButton{
//马克:公共财产
@i可检测的无功灵敏度Fdrag:CGFloat=5
//马克:私人物业
私人var startDragPoint:CGPoint?
//MARK:-UIResponder
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
守卫让第一次触碰=触碰第一次{
返回
}
let location=firstTouch.location(in:self)
让previousLocation=firstTouch.previousLocation(in:self)
如果startDragPoint==nil{
startDragPoint=以前的位置
}
if shouldAllowForSendActionForPoint(位置:位置){
super.touchesMoved(touches,with:event)
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
startDragPoint=nil
super.touchesend(触摸,带有:事件)
}
//MARK:-私有方法
private func应允许ForSendActionForPoint(位置:CGPoint)->Bool{
警卫让startDragPoint=startDragPoint else{
返回错误
}
设xDifferent=abs(startDragPoint.x-location.x)
设yDifferent=abs(startDragPoint.y-location.y)
返回xDifferent>sensitivityOfDrag | | yddifferent>sensitivityOfDrag
}
}
如果里面有一个简单的触摸拖动选项,那就太好了,但它工作正常,不会干扰我的其他连接。非常感谢您的h